简体   繁体   English

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

[英]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>&#10;</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.

相关问题 如何使用XSLT 1.0填补XML列表中的漏洞? - How can I use XSLT 1.0 to fill out the holes in my XML list? 如何使用XSLT 1.0将结构添加到非分层XML文件中? - How can I use XSLT 1.0 to add structure to a non-heirarchal XML file? 我该如何使用<xsl:param> &amp;<xsl:with-param> 使用 XSLT 1.0 将节点复制到新的 XML</xsl:with-param></xsl:param> - how can I use <xsl:param> & <xsl:with-param> to copy a node into the new XML with XSLT 1.0 如何使用 xslt 1.0 在 xml 元素中划分字符串 - How can I divide string in xml element using xslt 1.0 在xslt 1.0中,如何使用替代方法代替replace()函数 - In xslt 1.0, how do I use an alternative for replace() function 如何使用 XSLT 1.0 中的“替换”功能从我的 XML 中删除特殊字符以获得正确的 JSON 输出? - How do I use the "replace" function in XSLT 1.0 to remove special characters from my XML to get the correct JSON output? xslt 1.0-使用xslt 1.0如何使xslt适用于多个文档元素 - xslt 1.0- Using xslt 1.0 how can I make the xslt work for multiple document elements 在哪里可以找到XSLT 1.0的XML模式? - where can I find the XML Schema for XSLT 1.0? 如何在XSLT 1.0中将数字/字母文本字符串分成两个XML元素? - How can I separate a number/alpha text string into two XML elements in XSLT 1.0? 如何使用XSLT1.0在XML文件中替换ID元素及其所有参考元素 - How can I replace an ID element AND all of its reference elements in an XML file using XSLT1.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM