这是一个进行包装的样式表(两者都是
div
entry
):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="section">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="Head">
<xsl:choose>
<xsl:when test="self::Head">
<div>
<title>
<xsl:apply-templates/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="ListItem1">
<entry>
<xsl:apply-templates select="current-group()"/>
</entry>
</xsl:for-each-group>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="ListItem1">
<primary>
<xsl:apply-templates/>
</primary>
</xsl:template>
<xsl:template match="ListItem2">
<secondary>
<xsl:apply-templates/>
</secondary>
</xsl:template>
<xsl:template match="ListItem3 | ListItem4">
<tertiary>
<xsl:apply-templates/>
</tertiary>
</xsl:template>
</xsl:stylesheet>
输出为
<?xml version="1.0" encoding="UTF-8"?>
<section>
<p>Trying text</p>
<div>
<title>Heading 1</title>
<entry>
<primary>item 1</primary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
<tertiary>item 4</tertiary>
<tertiary>item 4</tertiary>
</entry>
<entry>
<primary>item 1</primary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
<tertiary>item 4</tertiary>
<tertiary>item 4</tertiary>
<tertiary>item 4</tertiary>
<tertiary>item 4</tertiary>
</entry>
<entry>
<primary>item 1</primary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
<tertiary>item 4</tertiary>
<tertiary>item 4</tertiary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
</entry>
</div>
<div>
<title>Heading 2</title>
<entry>
<primary>item 1</primary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
<tertiary>item 4</tertiary>
</entry>
<entry>
<primary>item 1</primary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
<tertiary>item 4</tertiary>
</entry>
<entry>
<primary>item 1</primary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
<tertiary>item 4</tertiary>
<tertiary>item 4</tertiary>
<secondary>item 2</secondary>
<secondary>item 3</secondary>
</entry>
</div>
</section>
到目前为止,我还没有看到一个明确的解释
level
属性来自或哪些是将其添加到哪些元素的规则,它似乎使用
<xsl:template match="ListItem3">
<tertiary level="1">
<xsl:apply-templates/>
</tertiary>
</xsl:template>
<xsl:template match="ListItem4">
<tertiary level="2">
<xsl:apply-templates/>
</tertiary>
</xsl:template>
<xsl:template match="ListItem3 | ListItem4">
<tertiary>
<xsl:apply-templates/>
</tertiary>
</xsl:template>
为您的样本提供正确的结果。