Как я могу использовать функцию else в xslt 1.0 / xml - PullRequest
0 голосов
/ 11 февраля 2020
   <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 ItemClassificationCodeification * 8 »Elek, затем« Elect », затем» Elect 1002 *

Я попробовал следующее:

<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, когда et c .. но я не могу заставить его работать.

Ответы [ 2 ]

1 голос
/ 11 февраля 2020

Вот для чего нужны правила шаблона:

<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>
0 голосов
/ 11 февраля 2020

Попробуйте следующим образом:

<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.

...