使用具有不同节点的xslt复制两个元素

时间:2012-07-20 06:46:30

标签: xml xslt xslt-2.0

以下是我的输入XML

<First>
<practice-session note="yes">
    <lessons>13000</lessons>
    <lessons>15000</lessons>
    <lessons>note</lessons>
</practice-session>
<practice-repeats note="yes">>
    <repeat>20000</repeat>
    <repeat>15000</repeat>
    <repeat>description</repeat>
</practice-repeats>
</First>

上述输出是

<chapters>
<chapter>
    <practice-session>
        <value>13000</value>
    </practice-session>
    <practice-repeats>
        <value>20000</value>
    </practice-repeats>
</chapter>
<chapter>
    <practice-session>
        <lesson>15000</lesson>
    </practice-session>
    <practice-repeats>
        <lesson>20000</lesson>
    </practice-repeats>
</chapter>
<chapter>
    <practice-session>
        <lesson>NOTE CHG</lesson>
    </practice-session>
    <practice-repeats>
        <lesson>ENG CHG</lesson>
    </practice-repeats>
</chapter>
</chapters>

当我在谷歌和堆栈溢出时,人们建议使用xsl:copy-of但是当我使用xsl时:复制我将如何照顾每个练习 - 会话将重复练习。

任何人都可以帮助我。

提前致谢

此致 Karthic

1 个答案:

答案 0 :(得分:1)

我认为不需要复制。试试这个......

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />  

<xsl:template match="/">
 <chapters>
   <xsl:apply-templates select="First/practice-session/lessons"/>
 </chapters>
</xsl:template>

<xsl:template match="lessons">
<chapter>
 <practice-session>
  <value><xsl:value-of select="." /></value>
 </practice-session>
 <practice-repeats>
   <value><xsl:value-of select="
     ../following-sibling::practice-repeats[1]/
     repeat[ count( current()/preceding-sibling::lessons) + 1]" /></value>
 </practice-repeats>
</chapter>
</xsl:template>

</xsl:stylesheet>