【问题标题】:How can I use the correct Max function in my code?如何在我的代码中使用正确的 Max 函数?
【发布时间】: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 个值吗?

标签: xml xslt xslt-1.0


【解决方案1】:

我认为您可以在没有扩展功能的情况下做到这一点。您当前的代码似乎表明您可以拥有多个 Percent(或者可能根本没有)。如果是这样,试试这个模板

<xsl:template match="/">
  <xsl:for-each select="/x:Invoice/cac:InvoiceLine/cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory">
    <xsl:sort select="cbc:Percent * (count(cbc:Percent) = 1) + 21 * (1 - (count(cbc:Percent) = 1))" order="descending" />
    <xsl:if test="position() = 1">
      <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:if>
  </xsl:for-each>
</xsl:template>

如果你只能有百分之零或百分之一,你可以将其简化为...

<xsl:sort select="cbc:Percent * count(cbc:Percent) + 21 * count(cbc:Percent)" order="descending" />

当然,如果所有节点都只有一个Percent,你可以这样做......

<xsl:sort select="cbc:Percent" />

【讨论】:

  • 谢谢您!所有节点都有一个百分比,所以我使用最后一个规则进行排序。那是为我做的。非常非常感谢.. 这拯救了我的一天。
【解决方案2】:

仅仅因为这不适合 cmets,我会以这种方式重构解决方案:

<xsl:template match="/">
  <xsl:variable 
     name="vTaxCategory"
     select="/x:Invoice/cac:InvoiceLine/cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory"/>
  <xsl:variable 
     name="vTaxCategoryWithNoPercent"
     select="$vTaxCategory[not(cbc:Percent)]"/>
  <xsl:for-each select="$vTaxCategory/cbc:Percent">
    <xsl:sort data-type="number" order="descending" />
    <xsl:if test="position() = 1">
      <xsl:choose>
        <xsl:when test="$vTaxCategoryWithNoPercent and 21 > . ">21</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="format-number(cbc:Percent,'00')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

只要得到最大值然后替换它,以防缺少Percent并且小于21。

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 2022-11-18
    • 1970-01-01
    • 2019-03-07
    • 2021-04-27
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多