2017-03-01 85 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