如何在代码中使用正确的Max函数?

时间:2019-05-08 06:39:05

标签: xml xslt xslt-1.0

早上好,

我的代码中具有相同的节点,并且它们都具有其他百分比。.我只想要具有最高百分比的节点。我似乎无法正确编写我的代码。有什么办法可以使用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作为结果,而不是节点中的所有结果。.有人可以帮我提供代码吗?

2 个答案:

答案 0 :(得分:0)

我认为您可以在没有扩展功能的情况下执行此操作。您当前的代码似乎建议您可以有多个DbContext.Add()(或者可能根本没有)。如果是这样,请尝试使用此模板

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

答案 1 :(得分:0)

仅因为这不适合注释,所以我将以这种方式重构解决方案:

input = "It is 50°c today.  One range is 30°c-40°c and here is another 10°c -20°c"
matches = re.findall(r'\d+°c(?:\s*-\d+°c)?', input)
print(matches)

['50\xc2\xb0c', '30\xc2\xb0c-40\xc2\xb0c', '10\xc2\xb0c -20\xc2\xb0c']

只要获得最大值,然后替换它,以防丢失的<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> 并且小于21。