2013-03-11 125 views
4

我有下面的XML文档(由不同的当​​事人提供)XSLT:基于其他属性的名称特定的属性值的总和

<transactions> 
    <transaction TaxAType="11" TaxAValue="111" TaxBType="2" TaxBValue="222" TaxCType="3" TaxCValue="333"/> 
    <transaction TaxAType="11" TaxAValue="111" TaxBType="2" TaxBValue="222" TaxCType="3" TaxCValue="333"/> 
</transactions> 

我想编写一个将改变这些线与和XSLT文档例如,如果相应的税务类型='11',则增加税额*值。

这是不可能使用模板的东西吗?

XQuery中的name(),substring()等函数? 你对此有何看法?

+0

应的结果是什么样的?只有一个号码?一些XML与总数?你说“税*类型='11例如”。这是否意味着在您的示例XML中,不会汇总TaxBValue和TaxCValue? – JLRishe 2013-03-12 00:37:41

+0

好吧,我的想法是,我将一个10类型的所有值的总和存储在我的转换文档的属性中。例如 transformed-transactions-elements 2013-03-12 01:39:07

+0

这并不能说明问题。这个时候”10“类型是从哪里来的?你是如何得出结果5433的?你能描述一下吗?你想要做什么,详细的?给你在你的问题中输入的XML示例,应该输出什么? – JLRishe 2013-03-12 03:46:25

回答

13

看一看下面的XSLT:??

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

    Sum of TaxA where type = 11: 
    <xsl:value-of select="sum(transactions/transaction[@TaxAType='11']/@TaxAValue)" /> 

    Sum of all tax where type = 11: 
    <xsl:value-of select="sum(transactions/transaction[@TaxAType='11']/@TaxAValue) 
     + sum(transactions/transaction[@TaxBType='11']/@TaxBValue) 
     + sum(transactions/transaction[@TaxCType='11']/@TaxCValue)" /> 

    </xsl:template> 
</xsl:stylesheet> 

它计算TaxAValue的总和与税务节点与TaxAType = 11的所有税值的节点,和TYPE = 11

+0

This Works!Thank you ...我在想使用更通用的方法使用用户定义的功能,但作为架构预定义,这将工作! 谢谢! – 2013-03-13 15:08:10

0

这个xml格式不适合完成你要求的处理类型。 你最好的选择是使用JavaScript或其他语言的XML功能(C#,Java等)来提取你需要的细节。

使用JavaScript,你可以找到税*类型= 11的任何属性,然后获得下一个属性

+0

我相信它不是pu但是我可能已经找到了一种合理的方法来创建一个用户定义的函数,它将接收一个名为Tax的所有属性的节点列表?类型值为11,然后UDF会查看并计算“对应”是Tax?值。 – 2013-03-13 15:06:42

1

这里是一个有点繁琐,但一般方法的价值:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:param name="taxType" select="11" /> 

    <xsl:template match="transactions"> 
    <result> 
     <xsl:variable name="allValues" 
        select="transaction/@*[substring(local-name(), 5, 5) = 'Type'] 
              [. = $taxType]" /> 
     <xsl:call-template name="SumValues"> 
     <xsl:with-param name="items" select="$allValues" /> 
     </xsl:call-template> 
    </result> 
    </xsl:template> 

    <xsl:template name="SumValues"> 
    <xsl:param name="items" /> 
    <xsl:param name="total" select="0" /> 

    <xsl:choose> 
     <xsl:when test="not($items)"> 
     <xsl:value-of select="$total" /> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:variable name="currentItem" select="$items[1]" /> 
     <xsl:variable name="currentValue"> 
      <xsl:apply-templates select="$currentItem" mode="getMatchingValue" /> 
     </xsl:variable> 

     <xsl:call-template name="SumValues"> 
      <xsl:with-param name="items" select="$items[position() > 1]" /> 
      <xsl:with-param name="total" select="$total + $currentValue" /> 
     </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template match="@*" mode="getMatchingValue"> 
    <xsl:variable name="typeCodeLetter" select="substring(local-name(), 4, 1)" /> 
    <xsl:variable name="valueAttributeName" 
        select="concat('Tax', $typeCodeLetter, 'Value')" /> 
    <xsl:variable name="matchingValueAttribute" 
        select="../@*[local-name() = $valueAttributeName]" /> 

    <!-- Multiply by boolean to handle the case where the 
     attribute wasn't found--> 
    <xsl:value-of select="$matchingValueAttribute * 
          boolean($matchingValueAttribute)"/> 
    </xsl:template> 
</xsl:stylesheet> 

这应该能够以处理任意数量的Tax*Type(只要*是单个字符),并可将所需类型作为参数传入。

0

总和(交易/事务[@ TaxAType = '11' ]/@ TaxAValue | @TaxBvalue | @ TaxCvalue)