【发布时间】:2019-05-08 06:39:04
【问题描述】:
早安,
我的代码中有相同的节点,它们都有其他百分比。我只想要百分比最高的节点。我似乎无法正确获取我的代码。有什么办法可以在这上面使用max函数吗?
<xsl:template match="/">
<xsl:for-each select="/x:Invoice/cac:InvoiceLine/cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory">
<xsl:choose>
<xsl:when test="count(cbc:Percent) = 1">
<xsl:value-of select="format-number(cbc:Percent,'00')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="21"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
我尝试了上面的代码。现在我得到结果 0021212121。所以代码选择了 cbc:Percent 中的所有字段。
<InvoiceLine>
<ID>1</ID>
<InvoicedQuantity/>
<LineExtensionAmount currencyID="EUR">0.00</LineExtensionAmount>
<OrderLineReference>
<LineID>1</LineID>
<OrderReference>
<ID>351450.27</ID>
</OrderReference>
</OrderLineReference>
<TaxTotal>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">0.00</TaxableAmount>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxCategory>
<ID>NA</ID>
<Percent>0</Percent>
<TaxScheme/>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>685000 / 08.0005 /filiaal: 2666 / ref1:
`351450.27</Description>`
<SellersItemIdentification>
<ID/>
</SellersItemIdentification>
</Item>
<Price>
<PriceAmount currencyID="EUR">0.00</PriceAmount>
<BaseQuantity>1</BaseQuantity>
</Price>
</InvoiceLine>
<InvoiceLine>
<ID>2</ID>
<InvoicedQuantity>2</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">19.76</LineExtensionAmount>
<OrderLineReference>
<LineID>2</LineID>
<OrderReference>
<ID>351450.27</ID>
</OrderReference>
</OrderLineReference>
<TaxTotal>
<TaxAmount currencyID="EUR">4.15</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">19.76</TaxableAmount>
<TaxAmount currencyID="EUR">4.15</TaxAmount>
<TaxCategory>
<ID>Excluding</ID>
<Percent>21</Percent>
<TaxScheme/>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>BLABLA</Description>
<SellersItemIdentification>
<ID>96184323</ID>
</SellersItemIdentification>
</Item>
<Price>
<PriceAmount currencyID="EUR">9.88</PriceAmount>
<BaseQuantity>1</BaseQuantity>
</Price>
</InvoiceLine>
<InvoiceLine>
<ID>3</ID>
<InvoicedQuantity>10</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">12.00</LineExtensionAmount>
<OrderLineReference>
<LineID>3</LineID>
<OrderReference>
<ID>351450.27</ID>
</OrderReference>
</OrderLineReference>
<TaxTotal>
<TaxAmount currencyID="EUR">2.52</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">12.00</TaxableAmount>
<TaxAmount currencyID="EUR">2.52</TaxAmount>
<TaxCategory>
<ID>Excluding</ID>
<Percent>21</Percent>
<TaxScheme/>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>KONS. 2H LICHT D37</Description>
<SellersItemIdentification>
<ID>7040237023</ID>
</SellersItemIdentification>
</Item>
<Price>
<PriceAmount currencyID="EUR">3.39</PriceAmount>
<BaseQuantity>1</BaseQuantity>
</Price>
</InvoiceLine>
<InvoiceLine>
<ID>4</ID>
<InvoicedQuantity>10</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">44.10</LineExtensionAmount>
<OrderLineReference>
<LineID>4</LineID>
<OrderReference>
<ID>351450.27</ID>
</OrderReference>
</OrderLineReference>
<TaxTotal>
<TaxAmount currencyID="EUR">9.26</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">44.10</TaxableAmount>
<TaxAmount currencyID="EUR">9.26</TaxAmount>
<TaxCategory>
<ID>Excluding</ID>
<Percent>21</Percent>
<TaxScheme/>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>blabla</Description>
<SellersItemIdentification>
<ID>7041037023</ID>
</SellersItemIdentification>
</Item>
<Price>
<PriceAmount currencyID="EUR">12.37</PriceAmount>
<BaseQuantity>1</BaseQuantity>
</Price>
</InvoiceLine>
<InvoiceLine>
<ID>5</ID>
<InvoicedQuantity>1</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">25.00</LineExtensionAmount>
<OrderLineReference>
<LineID>6</LineID>
<OrderReference>
<ID>351450.27</ID>
</OrderReference>
</OrderLineReference>
<TaxTotal>
<TaxAmount currencyID="EUR">5.25</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">25.00</TaxableAmount>
<TaxAmount currencyID="EUR">5.25</TaxAmount>
<TaxCategory>
<ID>Excluding</ID>
<Percent>21</Percent>
<TaxScheme/>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>blabla</Description>
<SellersItemIdentification>
<ID>VRACHT</ID>
</SellersItemIdentification>
</Item>
<Price>
<PriceAmount currencyID="EUR">25.00</PriceAmount>
<BaseQuantity>1</BaseQuantity>
</Price>
</InvoiceLine>
在这段代码中,最高百分比是 21,所以我只想要 21 作为结果,而不是节点中的所有结果。有人可以帮我写代码吗??
【问题讨论】:
-
如果您使用 XPath 2 迁移到 XSLT 2,那么您将拥有
max函数,因此请查看 maxtoroq.github.io/xpath-ref/fn/max.html。要在 XSLT 1 中找到最大值,您需要对最后一项进行升序或降序排序。 -
事实上,该技术不能在纯 XSLT 1.0 中完成——它需要(广泛但不是普遍可用的)
xx:node-set()扩展来将排序操作的结果转换为节点集你可以索引到。 -
你能帮我解决这个问题吗?我可以将代码放入现有代码中并仅选择最后 2 个值吗?