我有两套XML文件,因为我想在输入xml文件中使用相同的XSL代码。
我的第一个xml文件是:
<Body>
<h1>Child1</h1>
<p>Class1</p>
<h2>Child2</h2>
<p>Class2</p>
<h3>Child3</h3>
<p>Class3</p>
<h4>Child4</h4>
<p>Class4</p>
</Body>
XSL I用于此:
<xsl:template match="Body">
<xsl:for-each-group select="*" group-starting-with="h1">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<xsl:choose>
<xsl:when test="self::h2">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h3">
<xsl:choose>
<xsl:when test="self::h3">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h4">
<xsl:choose>
<xsl:when test="self::h4">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
我得到了如下输出:
<topic id="topic_1">
<title>Child1</title>
<body>
<p>Class1</p>
</body>
<topic id="topic_2">
<title>Child2</title>
<body>
<p>Class2</p>
</body>
<topic id="topic_3">
<title>Child3</title>
<body>
<p>Class3</p>
</body>
<topic id="topic_4">
<title>Child4</title>
<body>
<p>Class4</p>
</body>
</topic>
</topic>
</topic>
</topic>
现在我的第二个XML是(没有h2):
<Body>
<h1>Child1</h1>
<p>Class1</p>
<h3>Child3</h3>
<p>Class3</p>
<h4>Child4</h4>
<p>Class4</p>
</Body>
我对第二个XML的预期输出将是:
<topic id="topic_1">
<title>Child1</title>
<body>
<p>Class1</p>
</body>
<topic id="topic_2">
<title/>
<body/>
<topic id="topic_3">
<title>Child3</title>
<body>
<p>Class3</p>
</body>
<topic id="topic_4">
<title>Child4</title>
<body>
<p>Class4</p>
</body>
</topic>
</topic>
</topic>
</topic>
因为我需要获得此输出。我如何更改上面提到的XSL代码。我不需要新的编码。我想编辑上面的XSL代码。任何人都可以帮助我。
提前致谢
答案 0 :(得分:0)
对于这个问题,答案就在这里:
我用于第二个xml的XSL是:
<xsl:template match="Body">
<xsl:for-each-group select="*" group-starting-with="h1">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<xsl:choose>
<xsl:when test="self::h2">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h3">
<xsl:choose>
<xsl:when test="self::h3">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h4">
<xsl:choose>
<xsl:when test="self::h4">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="Body[not(h2)]">
<xsl:for-each-group select="*" group-starting-with="h1">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h3">
<xsl:choose>
<xsl:when test="self::h3">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h4">
<xsl:choose>
<xsl:when test="self::h4">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>