使用XSLT 2.0进行HTML到XML的转换

时间:2017-03-01 12:52:32

标签: html xml xslt xslt-2.0

我需要使用 XSLT 2.0 将HTML文件转换为XML格式。 HTML文件仅包含<p>标记,其中 h1 h2 h3 ,。 。

<body>
    <p class='h1'>the fisr A</p>
    <p class='txt'>one</p>
    <p>tow</p>
    <p class='h2'>the sec B</p>
    <p class='txt'>theree</p>
    <p class='h2'>the sec sec B</p>
    <p class='txt'>the next text</p>
    <p class='h3'>the fisr C</p>
    <p class='txt'>four</p>
    <p class='txt'>five</p>
    <p class='h1'>the seccond A</p>
    <p class='txt'>the seccond txt</p>
    <p class='h2'>the second B</p>
    <p class='txt'>six</p>
    <p class='txt'>seven</p>
    <p class='h1'>the third A</p>
    <p class='txt'>eight</p>
    <p class='txt'>nine</p>    
</body>

我需要XML输出,如下所示

<book>
   <sectionA>
      <title>the fisr A</title>
      <p class="txt">one</p>
      <p>tow</p>
      <sectionB>
         <title>the sec B</title>
         <p class="txt">theree</p>
      </sectionB>
      <sectionB>
         <title>the sec sec B</title>
         <p class="txt">the next text</p>
         <sectionC>
            <title>the fisr C</title>
            <p class="txt">four</p>
            <p class="txt">five</p>
         </sectionC>
      </sectionB>
   </sectionA>
   <sectionA>
      <title>the seccond A</title>
      <p class="txt">the seccond txt</p>
      <sectionB>
         <title>the second B</title>
         <p class="txt">six</p>
         <p class="txt">seven</p>
      </sectionB>
   </sectionA>
   <sectionA>
      <title>the third A</title>
      <p class="txt">eight</p>
      <p class="txt">nine</p>
   </sectionA>
</book>

有人可以帮助我获得所需的输出吗?

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">        
    <xsl:template match="body">
        <book>
            <xsl:for-each-group select="p" group-starting-with="p[@class='h1']">
                <sectionA>
                    <title>
                        <xsl:value-of select="node()"/>
                    </title>
                    <xsl:for-each-group select="current-group() except ." group-starting-with="p[@class='h2']">
                        <xsl:choose>
                            <xsl:when test="self::p[@class='h2']">
                                <sectionB>
                                    <title>
                                        <xsl:value-of select="node()"/>
                                    </title>
                                    <xsl:for-each-group select="current-group() except ." group-starting-with="p[@class='h3']">
                                        <xsl:choose>
                                            <xsl:when test="self::p[@class='h3']">
                                                <sectionC>
                                                    <title>
                                                        <xsl:value-of select="node()"/>
                                                    </title>
                                                    <xsl:apply-templates select="current-group() except ."></xsl:apply-templates>
                                                </sectionC>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:apply-templates select="current-group()"></xsl:apply-templates>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:for-each-group>
                                </sectionB>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:apply-templates select="current-group()"></xsl:apply-templates>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each-group>

                </sectionA>
            </xsl:for-each-group>
        </book>
    </xsl:template>

    <xsl:template match="p">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>      <!-- added by edit -->