我按顺序排列元素。我需要根据sec值对元素进行分组。下面我提到了我的意见。
<body>
<title>first</title>
<entry><sec>s 2</sec><p><text>some text 1</text></p></entry>
<entry><sec>s 3</sec><p><text>some text 2</text></p></entry>
<section>section title</section>
<entry><sec>Sh 1</sec><p><text>some text 3</text></p></entry>
<entry><sec>Sh 1<sub>Pt 2</sub></sec><p><text>some text 4</text></p></entry>
<entry><sec>Sh 1<sub>item 1</sub></sec><p><text>some text 5</text></p></entry>
<entry><sec>s 8</sec><p><text>some text 2</text></p></entry>
<entry><sec>s 9</sec><p><text>some text 2</text></p></entry>
<entry><sec>Sh 2<sub>item 2</sub></sec><p><text>some text 10</text></p></entry>
<entry><sec>Sh 2</sec><p><text>some text 20</text></p></entry>
</body>
我期待以下输出。
<body>
<title>first</title>
<entry><sec>s 2</sec><p><text>some text 1</text></p></entry>
<entry><sec>s 3</sec><p><text>some text 2</text></p></entry>
<section>section title</section>
<group>
<entry><sec>Sh 1</sec><p><text>some text 3</text></p></entry>
<entry><sec>Sh 1<sub>Pt 2</sub></sec><p><text>some text 4</text></p></entry>
<entry><sec>Sh 1<sub>item 1</sub></sec><p><text>some text 5</text></p></entry>
</group>
<entry><sec>s 8</sec><p><text>some text 2</text></p></entry>
<entry><sec>s 9</sec><p><text>some text 2</text></p></entry>
<group>
<entry><sec>Sh 2<sub>item 2</sub></sec><p><text>some text 10</text></p></entry>
<entry><sec>Sh 2</sec><p><text>some text 20</text></p></entry>
</group>
</body>
请尽力帮助我。
答案 0 :(得分:0)
您可以尝试:
<xsl:template match="body">
<body>
<xsl:for-each-group select="entry"
group-adjacent="starts-with(sec, 'Sh ')">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<group>
<xsl:copy-of select="current-group()"/>
</group>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</body>
</xsl:template>
<强>输出强>
<body>
<entry><sec>s 2</sec><p><text>some text 1</text></p></entry>
<entry><sec>s 3</sec><p><text>some text 2</text></p></entry>
<group>
<entry><sec>Sh 1</sec><p><text>some text 3</text></p></entry>
<entry><sec>Sh 1<sub>Pt 2</sub></sec><p><text>some text 4</text></p></entry>
<entry><sec>Sh 1<sub>item 1</sub></sec><p><text>some text 5</text></p></entry>
</group>
<entry><sec>s 8</sec><p><text>some text 2</text></p></entry>
<entry><sec>s 9</sec><p><text>some text 2</text></p></entry>
<group>
<entry><sec>Sh 2<sub>item 2</sub></sec><p><text>some text 10</text></p></entry>
<entry><sec>Sh 2</sec><p><text>some text 20</text></p></entry>
</group>
</body>
答案 1 :(得分:0)
如果组中只有一个项目,则不想包装项目,请使用
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<xsl:copy>
<xsl:for-each-group select="entry" group-by="sec/text()[1]">
<xsl:choose>
<xsl:when test="current-group()[2]">
<group>
<xsl:apply-templates select="current-group()"/>
</group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>