2013-05-07 36 views
1

我有一些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

首先,你必須要找到你能解釋的規則。至少我不明白爲什麼bullet_list是letter_list的子元素,但number_list不是。 – 2013-05-07 15:32:36

+0

您可以使用XSLT 2.0還是僅限於1.0? – 2013-05-07 15:42:21

+0

@IanRoberts我可以使用XSLT 2.0。 @ hr_117 number_list不是letter_list的子項,因爲它是一個完全獨立的列表?標籤中的數字表示它的級別。 'list_1'是一個頂級列表,'list_2'是'list_1'中的一個子列表。 – jorbas 2013-05-07 20:13:06

回答

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>