1
我有這樣的XML,XSLT - 獲得原始屬性到結果樹
<doc>
<section type="Main_Content">
<p id="main">aa</p>
<p id="main">bb</p>
<p id="main">cc</p>
<p id="para1">dd</p>
<p id="main">ee</p>
<p id="main">ff</p>
<p id="para2">hh</p>
<p id="main">ii</p>
<p id="main">jj</p>
<p id="para1">xx</p>
<p id="main">yy</p>
</section>
<section type="Main_Chapter">
<p id="main">ii</p>
<p id="main">jj</p>
<p id="para1">xx</p>
<p id="main">yy</p>
<p id="main">zz</p>
</section>
</doc>
我的任務是小組根據ID上述內容=「PARA1」和id =「PARA2」的屬性,並添加一個節到每個組。我期望的輸出是
<doc>
<section type="Main_Content">
<p id="main">aa</p>
<p id="main">bb</p>
<p id="main">cc</p>
</section>
<section type="First para">
<p id="para1">dd</p>
<p id="main">ee</p>
<p id="main">ff</p>
</section>
<section type="Second para">
<p id="para2">hh</p>
<p id="main">ii</p>
<p id="main">jj</p>
</section>
<section type="First para">
<p id="para1">xx</p>
<p id="main">yy</p>
</section>
<section type="Main_Chapter">
<p id="main">ii</p>
<p id="main">jj</p>
</section>
<section type="First para">
<p id="para1">xx</p>
<p id="main">yy</p>
<p id="main">zz</p>
</section>
</doc>
的XSL代碼這個任務,
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="section">
<xsl:for-each-group select="p" group-starting-with="p[starts-with(@id, 'para')]">
<section type="{@type}">
<xsl:if test="current-group()[1][@id='para1']">
<xsl:attribute name="type" select="'First para'"/>
</xsl:if>
<xsl:if test="current-group()[1][@id='para2']">
<xsl:attribute name="type" select="'Second para'"/>
</xsl:if>
<xsl:apply-templates select="current-group()"/>
</section>
</xsl:for-each-group>
</xsl:template>
結果從上面的XSL得到的是,
<doc>
<section type="">
<p id="main">aa</p>
<p id="main">bb</p>
<p id="main">cc</p>
</section>
<section type="First para">
<p id="para1">dd</p>
<p id="main">ee</p>
<p id="main">ff</p>
</section>
<section type="Second para">
<p id="para2">hh</p>
<p id="main">ii</p>
<p id="main">jj</p>
</section>
<section type="First para">
<p id="para1">xx</p>
<p id="main">yy</p>
</section>
<section type="">
<p id="main">ii</p>
<p id="main">jj</p>
</section>
<section type="First para">
<p id="para1">xx</p>
<p id="main">yy</p>
<p id="main">zz</p>
</section>
</doc>
SO,因爲它似乎在結果除了原來的type
屬性沒有被複制到<section>
節點外,其它情況都是正確的。如何修改xsl以獲得原始type
屬性值到<section>
節點?