2014-02-27 76 views
0

我有一些令人不安的問題,正在總結與每個元素有關的arraySize屬性數字。XSLT:以前所有屬性的總和

XML代碼:

<head> 
    <element> 
     <message name="something"> 
      <field arraySize="1"/> 
      <struct name="asdf"> 
       <struct name="qwera"> 
        <field arraySize="1"/> 
        <field arraySize="1"/>     
       </struct> 
       <struct name="xcv"> 
        <field arraySize="3"/> 
        <field arraySize="1"/> 
       </struct> 
       <struct name="nnge"> 
        <struct name="sdfssk"> 
         <field arraySize="1"/> 
         <field arraySize="1"/>     
        </struct> 
        <struct name="fhjmn"> 
         <field arraySize="2"/> 
         <field arraySize="1"/> 
        </struct> 
        <struct name="wetryk"> 
         <field arraySize="1"/> 
         <field arraySize="1"/> 
        </struct> 
       </struct> 
      </struct> 
      <field arraySize="1"/> 
     </message> 
    </element> 
    <element> 
     ... similar struct "tree" 
    </element> 
    <element> 
     ... similar struct "tree" 
    </element> 
</head> 

XSLT代碼:這是怎麼了,我試圖解決這個問題。

<xsl:template match="p:struct"> 
    <xsl:apply-templates> 
     <xsl:with-param name="previous" select="sum(preceding-sibling::*//@arraySize)"/> 
    </xsl:apply-templates> 
</xsl:template> 

<xsl:template match="p:field"> 
    <xsl:param name="previous" select="0"/> 
    <xsl:value-of select="$previous + sum(preceding-sibling::*//@arraySize)"/> 
</xsl:template> 

預期輸出:

element #1 
1 
2 
3 
6 
7 
8 
9 
11 
12 
13 
14 
15 

element #2 
1 
2 
... etc 

實際輸出:

1 <-- Problem #1 
1 
2 
5 
6 
1 <--- Problem #2 
2 
4 
5 
6 
7 
15 <-- The correct summation is produced here. 

我需要總結的所有preseding ARRAYSIZE的屬性。它有點作用,但是兩個問題是:1.第一個領域沒有總結。 2.總和重新開始於第三個縮進結構(如果縮進是正確的術語)。

有人可以幫助我嗎?

+0

你能告訴你真正希望在這個輸出案件?謝謝! –

+0

哦,對不起。忘了那個。我編輯了這篇文章。 –

回答

0

對此,我認爲您應該使用preceding::軸而不是preceding-sibling::軸。

像這樣:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:p="aaa"> 
    <xsl:output method="text" indent="yes"/> 

    <xsl:variable name="nl" select="'&#xA;'" /> 

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

    <xsl:template match="/*"> 
    <xsl:apply-templates select="p:element" /> 
    </xsl:template> 

    <xsl:template match="p:element"> 
    <xsl:value-of select="concat('element #', position(), $nl)"/> 
    <xsl:apply-templates /> 
    <xsl:value-of select="$nl"/> 
    </xsl:template> 

    <xsl:template match="p:field"> 
    <xsl:variable name="elId" select="generate-id(ancestor::p:element)" /> 
    <xsl:variable name="preds" 
        select="preceding::*/@arraySize[generate-id(ancestor::p:element) = 
                $elId]" /> 
    <xsl:value-of select="concat(@arraySize + sum($preds), $nl)"/> 
    </xsl:template> 
</xsl:stylesheet> 

產生輸出:

element #1 
1 
2 
3 
6 
7 
8 
9 
11 
12 
13 
14 
15 

element #2 
1 
2 
3 
+0

謝謝您的回覆,但我只注意到我忽略了一些細節而犯了相當大的錯誤。有多個元素節點,它只應該總結其內部的屬性。我很抱歉,並希望你仍然可以提供一些幫助。 –

+0

@ user3010546但是現在你的XML源缺少一個根元素,所以它是無效的。 –

+0

對不起。根添加。 –

0

這裏的另一種方式,你可以看看它:

<xsl:template match="/"> 
<xsl:for-each select="head/element"> 
    <xsl:variable name="elementID" select="generate-id()" /> 
    <xsl:value-of select="concat('element #', position(), '&#10;')"/> 
    <xsl:for-each select=".//field"> 
     <xsl:value-of select="@arraySize + sum(preceding::field[generate-id(ancestor::element)=$elementID]/@arraySize)" /> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:for-each> 
</xsl:for-each> 
</xsl:template>