2012-10-20 82 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