2017-03-01 103 views
-3

我需要使用XSLT 2.0将HTML文件转换为XML格式。该HTML文件仅包含<p>标签,其类别为h1,h2,h3,...。 。 。使用XSLT 2.0进行HTML到XML转换

<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> 

谁能帮我得到所需的输出?

+0

看一看的第三个例子在这里:https://www.w3.org/TR/xslt20/#grouping-examples –

+0

与为它去'的'并实现你的逻辑。这种苛刻的评论是因为你缺乏“自己尝试”和“让社区为我编写代码”。 SO是为了帮助,而不是为了编码请求。 – uL1

+0

我尝试使用for-each-group,但无法提供预期的输出 – Reegan

回答

1

你可以试试这个:

<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 --> 
+0

代码工作正常。谢谢 – Reegan