2011-05-10 73 views
2

我有一个xsl样式表只给出需要什么,除了在标签之外输出值之外。有没有办法删除它们?情景是,期望的输出是多次出现的发票的总发票金额。每次执行xslt时,参数p1都将InvoiceNumber包含在总数中。下面的代码显示参数p1硬编码为'351510'。XSLT如何删除不需要的输出

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/Invoices/Invoice[InvoiceNumber=351510][1]/InvoiceNumber"> 
     <xsl:copy> 
      <xsl:apply-templates select="/Invoices/Invoice[InvoiceNumber=351510][1]/InvoiceAmount"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:param name="tempvar"/> 
      <xsl:template name="InvTotal" match="/Invoices/Invoice[InvoiceNumber=351510][1]/InvoiceNumber"> 
     <xsl:variable name="p1" select="351510" />   
     <xsl:if test="/Invoices/Invoice/InvoiceNumber[. = $p1]"> 
     <!--<xsl:if test="$test = $p1" >--> 
      <InvoiceAmount> 
       <xsl:value-of select="sum(../../Invoice[InvoiceNumber=351510]/InvoiceAmount)"/> 
      </InvoiceAmount> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

这里是输入:

<Invoices> 
- <Invoice> 
    <InvoiceNumber>351510</InvoiceNumber> 
    <InvoiceAmount>137.00</InvoiceAmount> 
    </Invoice> 
- <Invoice> 
    <InvoiceNumber>351510</InvoiceNumber> 
    <InvoiceAmount>363.00</InvoiceAmount> 
    </Invoice> 
- <Invoice> 
    <InvoiceNumber>351511</InvoiceNumber> 
    <InvoiceAmount>239.50</InvoiceAmount> 
    </Invoice> 
    </Invoices> 

这里是输出:

<InvoiceAmount>500</InvoiceAmount>137.00351510363.00351511239.50 

下面是所需的输出:

<InvoiceAmount>500</InvoiceAmount> 

此外,谢谢你去lwburk谁让我这么远。

Problem is that the select statement xpath is not picking out the 'CarrierInvoiceAmount'. This might work though if I can get the path correct.

+0

好问题,+1。查看我的答案,获得完整,简短的解决方案和广泛的解释。 – 2011-05-11 03:26:05

回答

1

添加

<xsl:template match="text()"/> 

应该有所帮助。

+0

我加了'',不知道这是不是你的意思。谢谢。 – user745798 2011-05-10 17:37:39

1

我没有得到和你发布的结果相同的结果(只有351510137.00351510363.00351511239.50,所有的文本节点),我不知道tempvar(未使用)的目的。

由于看来,所有你需要的是InvoiceAmount值之和为特定InvoiceNumber,只是保持简单,忽略一切:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:param name="invoiceNumber"/> 
    <xsl:template match="/"> 
     <InvoiceAmount> 
      <xsl:value-of select="sum(/Invoices/Invoice[InvoiceNumber=$invoiceNumber]/InvoiceAmount)"/> 
     </InvoiceAmount> 
    </xsl:template> 
</xsl:stylesheet> 

您可以通过InvoiceNumber通过参数invoiceNumber处理,或者你可以硬编码它,如果你喜欢(见第1版)。

注意:如果您喜欢数字格式,例如: #.00(固定小数)为总和,那么你也可以使用format-number(…) function

+0

我在原帖中添加了一张图片,显示了您的建议结果。悬停在黄线上时,值为'0'。 – user745798 2011-05-10 20:41:59

+0

如果不是命名空间,我怀疑空白是这里的问题:我注意到你使用了''351510'',而我使用'351510'(没有引号)。如果你真的想保留报价,你可以使用'normalize-space(CarrierInvoiceNumber)='351510''。 – mousio 2011-05-10 20:52:38

1

这种转变

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pNum" select="351510"/> 

<xsl:key name="kInvAmmtByNumber" match="InvoiceAmount" 
      use="../InvoiceNumber"/> 

<xsl:variable name="vInvoiceAmounts" select= 
    "key('kInvAmmtByNumber', $pNum)"/> 

<xsl:variable name="vIdInvAmount1" select= 
    "generate-id($vInvoiceAmounts[1])"/> 

<xsl:template match="InvoiceAmount"> 
    <xsl:if test="generate-id() = $vIdInvAmount1"> 
    <InvoiceAmount> 
    <xsl:value-of select="sum($vInvoiceAmounts)"/> 
    </InvoiceAmount> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="text()"/> 
</xsl:stylesheet> 

时所提供的XML文件应用:

<Invoices> 
    <Invoice> 
     <InvoiceNumber>351510</InvoiceNumber> 
     <InvoiceAmount>137.50</InvoiceAmount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNumber>351510</InvoiceNumber> 
     <InvoiceAmount>362.50</InvoiceAmount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNumber>351511</InvoiceNumber> 
     <InvoiceAmount>239.50</InvoiceAmount> 
    </Invoice> 
</Invoices> 

正好产生想要的,正确的结果

<InvoiceAmount>500</InvoiceAmount> 

说明

  1. 的通缉发票号码传递给改造为外部/全局参数$pNum的价值。

  2. 我们使用一个键索引所有InvoiceAmount元素的对应InvoiceNumber值。

  3. 使用该密钥我们定义变量$vInvoiceAmounts包含节点集合的所有InvoiceAmount元件,其相应的InvoiceNumber元素的值是相同的外部参数$pNum的值。

  4. 我们还定义了一个变量($vIdInvAmount1),它包含第一个这样的InvoiceAmount元素的唯一标识。

  5. 有一个匹配任何InvoiceAmount元素的模板。它检查匹配的元素是否是包含在节点集$vInvoiceAmounts中的第一个元素。如果是这样,则使用单个文本节点子元素生成一个InvoiceAmount元素,其值是$vInvoiceAmounts中包含的所有InvoiceAmount元素的总和。否则什么都不做。

  6. 最后,还有第二个模板与任何文本节点相匹配,并且什么也不做(在输出中删除它),从而有效地覆盖了默认XSLT处理的不需要的副作用 - 输出不需要的文本。