如何在 xslt 1.0 / xml 中使用 else 函数


<cac:InvoiceLine xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">4</cbc:ID>
<cbc:InvoicedQuantity xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" unitCode="KWH">10825.00000000</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" currencyID="EUR">405.94</cbc:LineExtensionAmount>
<cac:InvoicePeriod>
<cbc:StartDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2020-01-01</cbc:StartDate>
<cbc:EndDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2020-01-08</cbc:EndDate>
</cac:InvoicePeriod>
<cac:Item>
<cbc:Description xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Opslag duurzame energie (schijf 2)</cbc:Description>
<cbc:Name xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Opslag duurzame energie (schijf 2)</cbc:Name>
<cac:CommodityClassification>
<cbc:ItemClassificationcode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" listID="ZZZ">83101800</cbc:ItemClassificationcode>
</cac:CommodityClassification>
<cac:ClassifiedTaxCategory>
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">S</cbc:ID>
<cbc:Percent xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">21.00</cbc:Percent>
<cac:TaxScheme>
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">VAT</cbc:ID>
</cac:TaxScheme>
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" currencyID="EUR">0.0375000</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
<cac:InvoiceLine xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">5</cbc:ID>
<cbc:InvoicedQuantity xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" unitCode="ANN">-0.02185792</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" currencyID="EUR">-9.52</cbc:LineExtensionAmount>
<cac:InvoicePeriod>
<cbc:StartDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2020-01-01</cbc:StartDate>
<cbc:EndDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2020-01-08</cbc:EndDate>
</cac:InvoicePeriod>
<cac:Item>
<cbc:Description xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Netbeheerkosten</cbc:Description>
<cbc:Name xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Netbeheerkosten</cbc:Name>
<cac:CommodityClassification>
<cbc:ItemClassificationcode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" listID="ZZZ">83101800</cbc:ItemClassificationcode>
</cac:CommodityClassification>
<cac:ClassifiedTaxCategory>
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">S</cbc:ID>
<cbc:Percent xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">21.00</cbc:Percent>
<cac:TaxScheme>
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">VAT</cbc:ID>
</cac:TaxScheme>
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" currencyID="EUR">435.6800000</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
<cac:InvoiceLine xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">1</cbc:ID>
<cbc:InvoicedQuantity xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" unitCode="MON">0.25810000</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" currencyID="EUR">0.00</cbc:LineExtensionAmount>
<cac:InvoicePeriod>
<cbc:StartDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2020-01-01</cbc:StartDate>
<cbc:EndDate xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2020-02-08</cbc:EndDate>
</cac:InvoicePeriod>
<cac:Item>
<cbc:Description xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Termijnbedrag Elektriciteit</cbc:Description>
<cbc:Name xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Termijnbedrag Elektriciteit</cbc:Name>
<cac:CommodityClassification>
<cbc:ItemClassificationcode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" listID="ZZZ">83101800</cbc:ItemClassificationcode>
</cac:CommodityClassification>
<cac:ClassifiedTaxCategory>
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">S</cbc:ID>
<cbc:Percent xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">21.00</cbc:Percent>
<cac:TaxScheme>
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">VAT</cbc:ID>
</cac:TaxScheme>
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" currencyID="EUR">0.0000000</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
</ubl:Invoice>

在上面你可以找到我得到的一段XML。我想要以下内容:如果 ItemClassificationCode 是 83101800 并且 Item/Description 是 "Termijnbedrag Elektriciteit" 那么 "Elektra1" Elseif ItemClassificationCode 是 83101800 并且 Item/Description 是 "Netbeheerkosten" 那么 "Net1" Elseif ItemClassificationCode 是83101800那么 "Elektra" Else ">

我尝试了以下方法:

<xsl:template match="/">
<xsl:for-each select="/*[local-name()='InvoiceLine']">
<xsl:if test="*[local-name()='Item']/*[local-name()='CommodityClassification']/*[local-name()='ItemClassificationcode']='83101800'">Elektra</xsl:if>
<xsl:if test="*[local-name()='Item']/*[local-name()='CommodityClassification']/*[local-name()='ItemClassificationcode']='83101800' and *[local-name()='Item']/*[local-name()='Description']='Netbeheerkosten'">Net1</xsl:if>
<xsl:if test="*[local-name()='Item']/*[local-name()='CommodityClassification']/*[local-name()='ItemClassificationcode']='83101800' and *[local-name()='Item']/*[local-name()='Description']='Termijnbedrag Elektriciteit'">Elektra1</xsl:if>
</xsl:for-each>
</xsl:template>

但是,上面的代码只是从所有内容中获取结果。所以 ElektraNet1Elektra1,但我只想要 Net1 或 Elektra 1,如果 InvoiceLine 有两个变量都依赖于它。因此,如果项代码83101800并且描述显示Netbeheerkosten,那么它应该只是Net1和Net1。

有人可以帮我吗?我也尝试过xsl:otherwhise 当等。但我似乎没有让它工作。

这就是模板规则的用途:

<xsl:template match="/ubl:Invoice">
<xsl:apply-templates select="cac:InvoiceLine[cac:Item/cac:CommodityClassification/cbc:ItemClassificationcode='83101800']">
</xsl:template>
<xsl:template match="cac:Item[cbc:Description='Netbeheerkosten']">Net1</xsl:template>
<xsl:template match="cac:Item[cbc:Description='Termijnbedrag Elektriciteit']">Net1</xsl:template>
<xsl:template match="cac:Item">Elektra</xsl:template>

试试这个方式:

<xsl:template match="/ubl:Invoice">
<xsl:for-each select="cac:InvoiceLine[cac:Item/cac:CommodityClassification/cbc:ItemClassificationcode='83101800']">
<xsl:choose>
<xsl:when test="cac:Item/cbc:Description='Netbeheerkosten'">Net1</xsl:when>
<xsl:when test="cac:Item/cbc:Description='Termijnbedrag Elektriciteit'">Elektra1</xsl:when>
<xsl:otherwise>Elektra</xsl:otherwise>
</xsl:choose>
<xsl:text>&#10;</xsl:text>
</xsl:for-each>
</xsl:template>

这要求样式表包含源 XML 中使用的命名空间声明。

相关内容

  • 没有找到相关文章

最新更新