[英]How can i use an else function in xslt 1.0 / xml
<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>
Above you can find an piece of the XML i got.在上面你可以找到我得到的一段 XML。 I want the following: if ItemClassificationCode is 83101800 and Item/Description is "Termijnbedrag Elektriciteit" then "Elektra1" Elseif ItemClassificationCode is 83101800 and Item/Description is "Netbeheerkosten" Then "Net1" Elseif ItemClassificationCode is 83101800 then "Elektra" Else ""我想要以下内容:如果 ItemClassificationCode 为 83101800 且 Item/Description 为“Termijnbedrag Elektriciteit”,则“Elektra1” Elseif ItemClassificationCode 为 83101800 且 Item/Description 为“Netbeheerkosten” 然后“Net1” Elseif ItemClassificationCode 为“Elseif ItemClassificationCode1”Elseif8000
I tried the following:我尝试了以下方法:
<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>
However the code above just gets me the results from everything.然而,上面的代码只是让我从一切中得到结果。 So ElektraNet1Elektra1 but I just want Net1 or Elektra 1 if the InvoiceLine has both of the variables depending on it.所以 ElektraNet1Elektra1 但我只想要 Net1 或 Elektra 1 如果 InvoiceLine 有两个取决于它的变量。 So if the itemcode is 83101800 AND the description says Netbeheerkosten then it should be Net1 and Net1 only..因此,如果项目代码是 83101800 并且描述说 Netbeheerkosten 那么它应该是 Net1 和 Net1 only..
Can someone please help me?有人可以帮帮我吗? I tried it also with xsl:otherwhise when etc.. but I don't seem to get it to work.我也尝试过使用 xsl:otherwise 时等等......但我似乎没有让它工作。
This is what template rules are for:这是模板规则的用途:
<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>
Try it this way:试试这个方法:
<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> </xsl:text>
</xsl:for-each>
</xsl:template>
This requires your stylesheet to contain the namespace declarations used in the source XML.这要求您的样式表包含源 XML 中使用的命名空间声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.