2012-10-20 79 views
2

以下是<Validation>标记可以有标记的1 .... n的XML。通过XSLT操纵XML值

<Header> 
    ... 
    ... 
</Header> 
<Validation> 
    <Type>Classic</Type> 
    <Percentage>10</sPercentage> 
    <Failed> 
      <ID>CS-20192018</ID> 
      <Department>MF404</Department> 
      <ErrorDescription>Failed at server</ErrorDescription> 
    </Failed> 
    <Failed> 
      <ID>CS-3233333</ID> 
      <Department>MF404</Department> 
      <ErrorDescription>Failed at webservice</ErrorDescription> 
    </Failed> 
    </Validation> 
<Validation> 
     <Type>CheckMember</Type> 
     <Percentage>20</Percentage> 
     <Failed> 
      <ID>CS-4648902</ID> 
      <Department>MF404</Department> 
      <ErrorDescription>Data not available</ErrorDescription> 
     </Failed> 
    </Validation> 

要求

我只是想总结基础上,<Failed>数的百分比。

首先<Validation>标签:

Percentage * no of <Failed> count = Result1 
10 * 2 = 20 

<Validation>标签:

Percentage * no of <Failed> count = Result2 
20 * 1 = 20 

Total = Result1 + Result2 = 20 + 20 = 40 

因此,我想总百分比输出为40,我试着用xsl:call-template的方法,但它都吹了。

您能否与我分享代码片段来计算百分比总和?

回答

1

一,有不同的可能XSLT 1.0解决方案

0.1。非递归,使用xxx:node-set()功能:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ext="http://exslt.org/common"> 

<xsl:output method="text"/> 

<xsl:template match="/"> 
    <xsl:variable name="vrtfResults"> 
    <xsl:apply-templates/> 
    </xsl:variable> 

    <xsl:value-of select="sum(ext:node-set($vrtfResults)/*)"/> 
</xsl:template> 

<xsl:template match="Validation"> 
    <x><xsl:value-of select="Percentage * count(Failed)"/></x> 
</xsl:template> 
</xsl:stylesheet> 

当这个变换所提供的XML文档应用(校正为进行良好的形成):

<t> 
    <Header> 
    ... 
    ... 
    </Header> 
    <Validation> 
     <Type>Classic</Type> 
     <Percentage>10</Percentage> 
     <Failed> 
      <ID>CS-20192018</ID> 
      <Department>MF404</Department> 
      <ErrorDescription>Failed at server</ErrorDescription> 
     </Failed> 
     <Failed> 
      <ID>CS-3233333</ID> 
      <Department>MF404</Department> 
      <ErrorDescription>Failed at webservice</ErrorDescription> 
     </Failed> 
    </Validation> 
    <Validation> 
     <Type>CheckMember</Type> 
     <Percentage>20</Percentage> 
     <Failed> 
      <ID>CS-4648902</ID> 
      <Department>MF404</Department> 
      <ErrorDescription>Data not available</ErrorDescription> 
     </Failed> 
    </Validation> 
</t> 

有用,正确结果产生

40 

.2。使用基本递归:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ext="http://exslt.org/common"> 
    <xsl:output method="text"/> 

    <xsl:template match="/*"> 
      <xsl:apply-templates select="Validation[1]"/> 
    </xsl:template> 

    <xsl:template match="Validation[following-sibling::Validation]"> 
    <xsl:param name="pSum" select="0"/> 

    <xsl:apply-templates select="following-sibling::Validation[1]"> 
     <xsl:with-param name="pSum" select="$pSum +Percentage*count(Failed)"/> 
    </xsl:apply-templates> 
    </xsl:template> 

    <xsl:template match="Validation"> 
    <xsl:param name="pSum" select="0"/> 
    <xsl:value-of select="$pSum +Percentage*count(Failed)"/> 
    </xsl:template> 
</xsl:stylesheet> 

当这种转化是在同一个XML文档(以上)应用中,相同的正确的结果产生

40 

II。 XSLT 2.0(XPath 2.0中)溶液

也可以指定为一个XPath 2.0表达式并不以所有需要XSLT以下:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 

<xsl:template match="/*"> 
    <xsl:sequence select="sum(Validation/(Percentage*count(Failed)))"/> 
</xsl:template> 
</xsl:stylesheet> 

也产生所想要的,正确的结果

40