-1

下面是我的输入 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:copy 时,我将如何照顾每个练习会话将有练习重复。

有人能帮我一下吗。

提前致谢

问候卡西克

4

1 回答 1

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>
于 2012-07-20T07:51:00.910 回答