我有一些XML描述书籍如下:
<root>
<chapter>
<chapter_number>some chapter</chapter_number>
<chapter_title>some title</chapter_title>
<heading_1>some heading</heading_1>
<para>some plain text <italic>some italic text</italic> some more text</para>
<list_1_letter>item 1</list_1_letter>
<list_1_letter>item 2</list_1_letter>
<list_2_bullet>sub-item 1</list_2_bullet>
<list_2_bullet>sub-item 2</list_2_bullet>
<list_1_letter>item 3</list_1_letter>
<para>some other text</para>
<list_1_number>item 1</list_1_number>
<list_2_roman>sub-item 1</list_2_roman>
<list_2_roman>sub-item 2</list_2_roman>
<list_1_number>item 2</list_1_number>
<list_2_roman>sub-item 3</list_2_roman>
<list_2_roman>sub-item 4</list_2_roman>
</chapter>
</root>
列表需要包含在描述它们的元素中,但子列表应该是它们上面列表的子元素。在这种情况下,所需的输出将是:
<root>
<chapter>
<chapter_number>some chapter</chapter_number>
<chapter_title>some title</chapter_title>
<heading_1>some heading</heading_1>
<para>some plain text <italic>some italic text</italic> some more text</para>
<letter_list>
<list_1_letter>item 1</list_1_letter>
<list_1_letter>item 2</list_1_letter>
<bullet_list>
<list_2_bullet>sub-item 1</list_2_bullet>
<list_2_bullet>sub-item 2</list_2_bullet>
</bullet_list>
<list_1_letter>item 3</list_1_letter>
</letter_list>
<para>some other text</para>
<number_list>
<list_1_number>item 1</list_1_number>
<roman_list>
<list_2_roman>sub-item 1</list_2_roman>
<list_2_roman>sub-item 2</list_2_roman>
</roman_list>
<list_1_number>item 2</list_1_number>
<roman_list>
<list_2_roman>sub-item 3</list_2_roman>
<list_2_roman>sub-item 4</list_2_roman>
</roman_list>
</number_list>
</chapter>
</root>
从这里调整一些其他答案我可以包装列表,但我只能包装相同列表的序列,它将产生:
<number_list>
<list_1_number>item 1</list_1_number>
</number_list>
<roman_list>
<list_2_roman>sub-item 1</list_2_roman>
<list_2_roman>sub-item 2</list_2_roman>
</roman_list>
<number_list>
<list_1_number>item 2</list_1_number>
</number_list>
<roman_list>
<list_2_roman>sub-item 3</list_2_roman>
<list_2_roman>sub-item 4</list_2_roman>
</roman_list>
不完全是我追求的!事实证明,这比我在预期时所表现的要快得多。
答案 0 :(得分:0)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="*">
<xsl:copy>
<xsl:for-each-group select="node()" group-adjacent="starts-with(name(),'list')">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:element name="{replace(name(),'.*_.*_','')}_list">
<xsl:variable name="thislist" select="name()"/>
<xsl:for-each-group select="current-group()" group-adjacent="name()">
<xsl:choose>
<xsl:when test="name()=$thislist">
<xsl:copy-of select="current-group()"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{replace(name(),'.*_.*_','')}_list">
<xsl:copy-of select="current-group()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
可生产
<root>
<chapter>
<chapter_number>some chapter</chapter_number>
<chapter_title>some title</chapter_title>
<heading_1>some heading</heading_1>
<para>some plain text <italic>some italic text</italic> some more text</para>
<letter_list>
<list_1_letter>item 1</list_1_letter>
<list_1_letter>item 2</list_1_letter>
<bullet_list>
<list_2_bullet>sub-item 1</list_2_bullet>
<list_2_bullet>sub-item 2</list_2_bullet>
</bullet_list>
<list_1_letter>item 3</list_1_letter>
</letter_list>
<para>some other text</para>
<number_list>
<list_1_number>item 1</list_1_number>
<roman_list>
<list_2_roman>sub-item 1</list_2_roman>
<list_2_roman>sub-item 2</list_2_roman>
</roman_list>
<list_1_number>item 2</list_1_number>
<roman_list>
<list_2_roman>sub-item 3</list_2_roman>
<list_2_roman>sub-item 4</list_2_roman>
</roman_list>
</number_list>
</chapter>
</root>