如何在同一级别对一个标签下的类似标签进行分组?
我使用xslt 1.0
将样本结果与我的样本xsl粘贴在一起
只有彼此相邻的子部分需要组合在一起。如果同一级别的其他小节与前一小节不相邻,则需要单独分组......
先谢谢 这是我的输入xml
<body>
This is a generic section which is considered the first paragraph<para><paratext>this is a sub para</paratext></para>
<subsection>
<para>
<paratext>LIST ITEM 1</paratext>
</para>
</subsection>
<subsection>
<para>
<paratext>LIST ITEM 2</paratext>
</para>
<subsection>
<para>
<paratext>CHILD LISt ITEM2 ITEM 1 </paratext>
</para>
</subsection>
<subsection>
<para>
<paratext>CHILD LISt ITEM2 ITEM 2 </paratext>
</para>
</subsection>
</subsection>
</body>
这是我的预期结果
<text>
This is a generic section which is considered the first paragraph<p>this is a sub para </p>
<ul>
<li>LIST ITEM 1</li>
<li>LIST ITEM 2</li>
<ul>
<li>CHILD LIST ITEM2 ITEM1</li>
<li>CHILD LIST ITEM2 ITEM2</li>
</ul>
</ul>
</text>
使用以下xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/body">
<text>
<xsl:apply-templates />
</text>
</xsl:template>
<xsl:template match="/body/subsection">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="/body/subsection/subsection">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="/body/subsection/subsection/para/paratext">
<li>
<xsl:apply-templates />
</li>
</xsl:template>
<xsl:template match="/body/subsection/para/paratext">
<li>
<xsl:apply-templates />
</li>
</xsl:template>
<xsl:template match="body/para/paratext">
<p>
<xsl:apply-templates />
</p>
</xsl:template>
</xsl:stylesheet>
我能够得到以下结果
<?xml version="1.0" encoding="UTF-8"?>
<text>
This is a generic section which is considered the first paragraph<p>this is a sub para</p>
<ul>
<li>LIST ITEM 1</li>
</ul>`enter code here`
<ul>
<li>LIST ITEM 2</li>
<ul>
<li>CHILD LISt ITEM2 ITEM 1 </li>
</ul>
<ul>
<li>CHILD LISt ITEM2 ITEM 2 </li>
</ul>
</ul>
</text>
答案 0 :(得分:0)
这有很多关于格式的假设,但是这个例子是xml:
<body>
<subsection>
<para>
<paratext>LIST ITEM 1</paratext>
</para>
</subsection>
<subsection>
<para>
<paratext>LIST ITEM 2</paratext>
</para>
<subsection>
<para>
<paratext>LIST ITEM 2</paratext>
</para>
<para>
<paratext>LIST ITEM 2</paratext>
</para>
</subsection>
<subsection>
<para>
<paratext>LIST ITEM 2</paratext>
</para>
<para>
<paratext>LIST ITEM 2</paratext>
</para>
</subsection>
</subsection>
</body>
和这个例子xslt:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/body">
<text>
<ul>
<xsl:apply-templates />
</ul>
</text>
</xsl:template>
<xsl:template match="/body/subsection/subsection">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="paratext">
<li>
<xsl:apply-templates />
</li>
</xsl:template>
</xsl:stylesheet>
结果是:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<ul>
<li>LIST ITEM 1</li>
<li>LIST ITEM 2</li>
<ul>
<li>LIST ITEM 2</li>
<li>LIST ITEM 2</li>
</ul>
<ul>
<li>LIST ITEM 2</li>
<li>LIST ITEM 2</li>
</ul>
</ul>
</text>
答案 1 :(得分:0)
我希望你能从中获得最终解决方案:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/body">
<text>
<xsl:apply-templates select="child::subsection[1]" />
</text>
</xsl:template>
<xsl:template match="subsection">
<ul>
<xsl:apply-templates select="child::para" />
<xsl:apply-templates select="following-sibling::subsection/para" />
<xsl:apply-templates select="../subsection/subsection[1]" />
</ul>
</xsl:template>
<xsl:template match="para">
<li>
<xsl:apply-templates />
</li>
</xsl:template>
</xsl:stylesheet>
答案 2 :(得分:0)
此样式表为第二级添加了另一个<ul>
级别。如果你不超过两个或三个级别,那么非常简单:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="body">
<text>
<ul>
<xsl:apply-templates select="subsection/para"/>
<ul><xsl:apply-templates select="subsection/subsection"/></ul>
</ul>
</text>
</xsl:template>
<xsl:template match="paratext">
<li><xsl:apply-templates /></li>
</xsl:template>
</xsl:stylesheet>
这是输出:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<ul>
<li>LIST ITEM 1</li>
<li>LIST ITEM 2</li>
<ul>
<li>CHILD LISt ITEM2 ITEM 1 </li>
<li>CHILD LISt ITEM2 ITEM 2 </li>
</ul>
</ul>
</text>
答案 3 :(得分:0)
此答案改编自How can I wrap a group of adjacent elements using XSLT?
考虑以下输入XML(我添加了额外的节点和第三级子部分):
<body>
<para>aaa</para>
<subsection>
<para>
<paratext>LIST ITEM 1</paratext>
</para>
</subsection>
<subsection>
<para>
<paratext>LIST ITEM 2</paratext>
</para>
<subsection>
<para>
<paratext>CHILD LISt ITEM2 ITEM 1 </paratext>
</para>
</subsection>
<subsection>
<para>
<paratext>CHILD LISt ITEM2 ITEM 2 </paratext>
</para>
<subsection>
<para>
<paratext>CHILD LISt ITEM2 ITEM 2 </paratext>
</para>
</subsection>
</subsection>
</subsection>
<para>bbb</para>
</body>
将以下样式表应用于上述XML时:
<?xml version="1.0" encoding="UTF-8"?>
<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:key name="kFollowing" match="subsection[preceding-sibling::*[1][self::subsection]]"
use="generate-id(preceding-sibling::subsection
[not(preceding-sibling::*[1][self::subsection])])"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<text>
<xsl:apply-templates/>
</text>
</xsl:template>
<xsl:template match="subsection
[not(preceding-sibling::*[1][self::subsection])]">
<ul>
<xsl:apply-templates/>
<xsl:apply-templates mode="copy" select="key('kFollowing', generate-id())"/>
</ul>
</xsl:template>
<xsl:template match="subsection[preceding-sibling::*[1][self::subsection]]"/>
<xsl:template match="subsection" mode="copy">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="para[paratext]">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template match="paratext">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
它产生:
<?xml version="1.0" encoding="utf-8"?>
<text>
<para>aaa</para>
<ul>
<li>LIST ITEM 1</li>
<li>LIST ITEM 2</li>
<ul>
<li>CHILD LISt ITEM2 ITEM 1 </li>
<li>CHILD LISt ITEM2 ITEM 2 </li>
<ul>
<li>CHILD LISt ITEM2 ITEM 2 </li>
</ul>
</ul>
</ul>
<para>bbb</para>
</text>