如何 select 在 XSLT 中的一个节点中只有一个元素?

How to select only one element in a node in XSLT?

我想 select 只有一个代码元素(具有最大级别值)与 XSLT 但它不起作用。

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <object>
    <metadata>
      <schemaVersion>54</schemaVersion>
      <offset>13058626</offset>
      <processId>9f151cd9-7039-4b1a-99de-8336c75f5f28</processId>
      <processDate>2020-11-03T12:31:33.239Z</processDate>
      <processApp>siriusPRT-uat</processApp>
      <version>1</version>
      <technicalId>24000098295098_PT_PT508012040</technicalId>
      <status>INTEGRATED</status>
      <group>pt</group>
    </metadata>
    <data>
      <tradeItemUnitDescriptorCode>BASE_UNIT_OR_EACH</tradeItemUnitDescriptorCode>
      <gtin>24000098295098</gtin>
      <additionalTradeItemIdentification>
        <additionalTradeItemIdentificationTypeCode>CUG</additionalTradeItemIdentificationTypeCode>
        <additionalTradeItemIdentificationValue>00982950</additionalTradeItemIdentificationValue>
      </additionalTradeItemIdentification>
      <additionalTradeItemIdentification>
        <additionalTradeItemIdentificationTypeCode>GTIN_13</additionalTradeItemIdentificationTypeCode>
        <additionalTradeItemIdentificationValue>2400009829509</additionalTradeItemIdentificationValue>
      </additionalTradeItemIdentification>
      <zordaDedicatedInformation>
        <tradeItemStatus>A</tradeItemStatus>
        <zordaSupplyChain>
          <zordaSupplierCode>Q370</zordaSupplierCode>
        </zordaSupplyChain>
        <mainGtin>-1</mainGtin>
        <cug>00982950</cug>
        <isMainGtin>false</isMainGtin>
        <countryOfRecordCountryCode>PT</countryOfRecordCountryCode>
        <zordaClassification>
          <groupingCode>PT</groupingCode>
          <code>08</code>
          <hierarchyType>Product</hierarchyType>
          <level>1</level>
          <variantCode>PT</variantCode>
          <hierarchyCode>REF</hierarchyCode>
          <concatenatedCode>08</concatenatedCode>
          <description>
            <pt>ADMISTRATIVOS</pt>
          </description>
          <applicationDate>2010-06-23T00:00:00+02:00</applicationDate>
        </zordaClassification>
        <zordaClassification>
          <groupingCode>PT</groupingCode>
          <code>0888</code>
          <hierarchyType>Product</hierarchyType>
          <level>2</level>
          <variantCode>PT</variantCode>
          <hierarchyCode>REF</hierarchyCode>
          <concatenatedCode>080888</concatenatedCode>
          <description>
            <pt>REC. PAGTº P/ ENTIDADES EXTERN</pt>
          </description>
          <applicationDate>2010-06-23T00:00:00+02:00</applicationDate>
        </zordaClassification>
        <zordaClassification>
          <groupingCode>PT</groupingCode>
          <code>088802</code>
          <hierarchyType>Product</hierarchyType>
          <level>3</level>
          <variantCode>PT</variantCode>
          <hierarchyCode>REF</hierarchyCode>
          <concatenatedCode>080888088802</concatenatedCode>
          <description>
            <pt>ENTIDADE EXTERNA NSBE</pt>
          </description>
          <applicationDate>2010-06-23T00:00:00+02:00</applicationDate>
        </zordaClassification>
        <zordaClassification>
          <groupingCode>PT</groupingCode>
          <code>08880202</code>
          <hierarchyType>Product</hierarchyType>
          <level>4</level>
          <variantCode>PT</variantCode>
          <hierarchyCode>REF</hierarchyCode>
          <concatenatedCode>08088808880208880202</concatenatedCode>
          <description>
            <pt>CONSULTAS DE ESTéTICA</pt>
          </description>
          <applicationDate>2010-06-23T00:00:00+02:00</applicationDate>
        </zordaClassification>
        <isMainSupplier>false</isMainSupplier>
        <companyInternalIdentification>PT508012040</companyInternalIdentification>
        <isTradeItemAuchanBrand>false</isTradeItemAuchanBrand>
      </zordaDedicatedInformation>
      <isTradeItemAService>false</isTradeItemAService>
      <tradeItemSynchronisationDates>
        <effectiveDateTime>2010-06-23T00:00:00+02:00</effectiveDateTime>
        <discontinuedDateTime>2100-12-31T00:00:00+02:00</discontinuedDateTime>
        <lastChangeDateTime>2020-11-03T12:17:06+02:00</lastChangeDateTime>
      </tradeItemSynchronisationDates>
      <isTradeItemABaseUnit>true</isTradeItemABaseUnit>
      <isTradeItemADespatchUnit>false</isTradeItemADespatchUnit>
      <isTradeItemAConsumerUnit>true</isTradeItemAConsumerUnit>
      <tradeItemInformation>
        <extension>
          <variableTradeItemInformationModule>
            <variableTradeItemInformation>
              <variableTradeItemTypeCode>LOOSE</variableTradeItemTypeCode>
              <isTradeItemAVariableUnit>false</isTradeItemAVariableUnit>
            </variableTradeItemInformation>
          </variableTradeItemInformationModule>
          <deliveryPurchasingInformationModule>
            <deliveryPurchasingInformation>
              <endAvailabilityDateTime>2100-12-31T00:00:00+02:00</endAvailabilityDateTime>
              <orderingUnitOfMeasure>MM</orderingUnitOfMeasure>
            </deliveryPurchasingInformation>
          </deliveryPurchasingInformationModule>
          <dutyFeeTaxInformationModule>
            <dutyFeeTaxInformation>
              <dutyFeeTaxTypeCode>03</dutyFeeTaxTypeCode>
            </dutyFeeTaxInformation>
          </dutyFeeTaxInformationModule>
          <tradeItemMeasurementsModule>
            <tradeItemMeasurements>
              <tradeItemWeight>
                <grossWeight>
                  <measurementValue>0</measurementValue>
                  <measurementUnitCode>KGM</measurementUnitCode>
                </grossWeight>
                <netWeight>
                  <measurementValue>0</measurementValue>
                  <measurementUnitCode>KGM</measurementUnitCode>
                </netWeight>
                <drainedWeight>
                  <measurementValue>0</measurementValue>
                  <measurementUnitCode>KGM</measurementUnitCode>
                </drainedWeight>
              </tradeItemWeight>
              <netContent>
                <measurementValue>0</measurementValue>
                <measurementUnitCode>EA</measurementUnitCode>
              </netContent>
            </tradeItemMeasurements>
          </tradeItemMeasurementsModule>
          <salesInformationModule>
            <salesInformation>
              <priceComparisonMeasurement>
                <measurementValue>0</measurementValue>
                <measurementUnitCode>GRM</measurementUnitCode>
              </priceComparisonMeasurement>
              <sellingUnitOfMeasure>EA</sellingUnitOfMeasure>
            </salesInformation>
          </salesInformationModule>
          <tradeItemDescriptionModule>
            <tradeItemDescriptionInformation>
              <tradeItemDescription>
                <pt>HOMEM ABDOMEN 60 MIN </pt>
              </tradeItemDescription>
              <brandNameInformation>null</brandNameInformation>
            </tradeItemDescriptionInformation>
          </tradeItemDescriptionModule>
          <placeOfItemActivityModule>
            <placeOfProductActivity>
              <productActivityDetails>
                <productActivityTypeCode>LAST_PROCESSING</productActivityTypeCode>
              </productActivityDetails>
              <countryOfOriginStatement>
                <pt>Portugal</pt>
              </countryOfOriginStatement>
              <countryOfOrigin>
                <countryCode>620</countryCode>
              </countryOfOrigin>
            </placeOfProductActivity>
          </placeOfItemActivityModule>
          <packagingMarkingModule>
            <packagingMarking>
              <isPriceOnPack>false</isPriceOnPack>
              <hasBatchNumber>false</hasBatchNumber>
            </packagingMarking>
          </packagingMarkingModule>
        </extension>
      </tradeItemInformation>
    </data>

我尝试使用排序,但没有得到我想要的输出(没有打印任何内容,我只需要一个输出,即具有最大级别的代码)。

我也在尝试使用 max 函数,但它提供了多个输出。我是 XSLT 和 xpath 的初学者,所以我可能使用了错误的函数...

这是我的 XSLT 代码:

<?xml version="1.0" encoding="UTF-8"?>

<!--Changelog :
0.1.0 @SJA : Initial verision
-->

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:param name="NomenclatureCode" select="1"/>


<xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match="/">
        <xsl:for-each select="root/object/data/zordaDedicatedInformation/zordaClassification">
            <xsl:sort select="level" order="ascending"/>
            <ProductCode><xsl:value-of select="../cug"/></ProductCode>
            <Delimited>;</Delimited>
            <NomenclatureCode><xsl:value-of select="$NomenclatureCode"/></NomenclatureCode>
            <Delimited>;</Delimited>
                <xsl:if test="code/position() = last()">
                    <ElementCode><xsl:value-of select="code"/></ElementCode>
                </xsl:if>
                <Delimited>;</Delimited>
            <xsl:if test="position() != last()"><xsl:text>&#10;</xsl:text></xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

我不知道你需要哪种输出,但这选择了你正在寻找的值:

XSLT 2.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:variable name="maxLevel" select="max(root/object/data/zordaDedicatedInformation/zordaClassification/level)"/>
    <xsl:value-of select="root/object/data/zordaDedicatedInformation/zordaClassification[level=$maxLevel]/code"/>
  </xsl:template>
  
</xsl:stylesheet>

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:variable name="levelMax">
        <xsl:for-each select="root/object/data/zordaDedicatedInformation/zordaClassification">
            <xsl:sort select="level" order="descending"/>
            <xsl:if test="position() = 1">
                <xsl:value-of select="level"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    
    <xsl:apply-templates select="root/object/data/zordaDedicatedInformation/zordaClassification[level=$levelMax]"/>
    
  </xsl:template>
  
  <xsl:template match="zordaClassification">
      <xsl:value-of select="code"/>
      <xsl:text>&#xa;</xsl:text> <!-- NewLine -->
  </xsl:template>
  
</xsl:stylesheet>

看到它在这里工作:https://xsltfiddle.liberty-development.net/gVrvcxt/2