2012-02-01 38 views
2

我有一個HTML文件,我要轉換爲XML只使用XSLT處理每個節點..通過ID動態排序節點,並使用XSLT

我想 1,所有的節點被保留。 2.所有元素都進行排序。 3.代碼應該是動態的。

我有一個巨大的文件,所以我希望有一個簡單的代碼來處理所有的HTML nodes.here我解釋了自己與XSLT codeing如果u瞭解plz幫助我..

我更新的HTML文件。 。

<div id="2196" class="one_tail"> 
<span id="2197" class="one_biblio"> 
    <span id="2198" class="one_section-title"><b>Title</b></span> 
    <ol id="2199" class="one_biblio-sec"> 
     <li id="2200" class="one_bib-reference"> 
      <span id="2202" class="one_reference"> 
       <span id="2203" class="one_contribution"> 
        <span id="2204" class="one_authors"> 
         <span id="2205" class="one_author"> 

          <!-- here the id value misplaced --> 
           <span id="2207" class="one_surname">Surname</span> 
          <span id="2206" class="one_given-name">GivenName</span> 

         </span> 
        </span> 
        <span id="2208" class="one_title"> 
         <span id="2209" class="one_maintitle">technology</span> 
        </span> 
       </span> 
       <span id="2210" class="one_host"> 
        <span id="2211" class="one_book"> 
         <span id="2213" class="one_publisher">Publisher </span> 
        </span> 
       </span>. 
      </span> 
     </li> 
    </ol> 
</span> 
</div> 

,我想的XML文件:這裏 屬性類值將用作元素名稱。

<tail id="2196"> 
<biblio id="2197" > 
    <section-title id="2198" ><b>Title</b></section-title> 
    <biblio-sec id="2199" > 
     <bib-reference id="2200" > 
      <reference id="2202" > 
       <contribution id="2203" > 
        <authors id="2204" > 
         <author id="2205" > 
              <!-- correrct the id --> 
           <given-name id="2206" >GivenName </given-name> 
          <surname id="2207" >Surname</surname> 
          </author> 
        </authors> 
         <title id="2208" > 
           <maintitle id="2209" >technology</maintitle> 
          </title> 
       </contribution> 
       </reference> 
      <host id="2210" > 
       <book id="2211" > 
        <publisher id="2213" >Publisher </publisher> 
        </book> 
       </host> 
      </bib-reference> 
       </biblio-sec> 
     </biblio> 
</tail> 

我寫的XSLT沒有給我想要的東西.. 的XSLT代碼:

<xsl:template match="*|/" 
    <xsl:for-each select="."> 
      <xsl:for-each select="current()/*"> 
       <xsl:sort select="@id" order="ascending"/> 
        </xsl:for-each> 
      <xsl:if test="starts-with(@class,'one_') "> 
        <xsl:variable name="nodename" select="substring-after(@class,'one_')"/> 
         <xsl:element name="{$nodename}"> 
          <xsl:attribute name="id" select="@id"/> 
         <xsl:apply-templates/> 
        </xsl:element> 
        </xsl:if> 
        <xsl:if test="not(@class)"> 
         <xsl:apply-templates/> 
        </xsl:if> 
        </xsl:for-each>      
      </xsl:template> 

任何一個可以幫助我..

回答

1

此轉換

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*[@class]"> 
    <xsl:element name="{substring-after(@class, 'one_')}"> 
    <xsl:copy-of select="@*[not(name()='class')]"/> 
    <xsl:apply-templates> 
    <xsl:sort select="@id" data-type="number"/> 
    </xsl:apply-templates> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<div id="2196" class="one_tail"> 
    <span id="2197" class="one_biblio"> 
     <span id="2198" class="one_section-title"> 
      <b>Title</b> 
     </span> 
     <ol id="2199" class="one_biblio-sec"> 
      <li id="2200" class="one_bib-reference"> 
       <span id="2202" class="one_reference"> 
        <span id="2203" class="one_contribution"> 
         <span id="2204" class="one_authors"> 
          <span id="2205" class="one_author"> 
           <!-- here the id value misplaced --> 
           <span id="2207" class="one_surname">Surname</span> 
           <span id="2206" class="one_given-name">GivenName</span> 
         </span> 
        </span> 
        <span id="2208" class="one_title"> 
          <span id="2209" class="one_maintitle">technology</span> 
        </span> 
       </span> 
        <span id="2210" class="one_host"> 
         <span id="2211" class="one_book"> 
          <span id="2213" class="one_publisher">Publisher </span> 
         </span> 
        </span>. 
       </span> 
      </li> 
     </ol> 
    </span> 
</div> 

產生想要的,正確的結果

<tail id="2196"> 
    <biblio id="2197"> 
     <section-title id="2198"> 
     <b>Title</b> 
     </section-title> 
     <biblio-sec id="2199"> 
     <bib-reference id="2200"> 
      <reference id="2202">. 
       <contribution id="2203"> 
        <authors id="2204"> 
        <author id="2205"><!-- here the id value misplaced --> 
         <given-name id="2206">GivenName</given-name> 
         <surname id="2207">Surname</surname> 
        </author> 
        </authors> 
        <title id="2208"> 
        <maintitle id="2209">technology</maintitle> 
        </title> 
       </contribution> 
       <host id="2210"> 
        <book id="2211"> 
        <publisher id="2213">Publisher </publisher> 
        </book> 
       </host> 
      </reference> 
     </bib-reference> 
     </biblio-sec> 
    </biblio> 
</tail> 
+0

謝謝..很多@Dimitre ..它工作得很好... – Ramesh 2012-02-02 04:00:52

+0

謝謝..謝謝很多Dimitre ..它工作得很好...而且我還有一個疑問 - 我有一些父母之外的值標籤,以便這些值在孩子面前如何避免它。在HTML我有一個 ,, ,,此之際, ,,,,, 一個我Wnt信號避開那些逗號(,,,,,)怎麼辦.. ..? plz幫助我.... – Ramesh 2012-02-02 04:09:55

+0

@Ramesh:不客氣。至於你的新需求到目前爲止還不清楚,有兩種方法可以解決這個問題:或者使用Update編輯你當前的問題,或者首選,提出一個新問題,並提供新的XML文檔以及對所需輸出的任何新需求。根據我的經驗,如果編輯並添加新的需求,則問題會變得太過重載。當您發佈新問題時,我會很高興閱讀並提供答案。 – 2012-02-02 04:30:03

1

下面是一個簡單的樣式表:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output indent="yes" method="xml"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*[@class]"> 
    <xsl:element name="{substring-after(@class, 'one_')}"> 
     <xsl:copy-of select="@id"/> 
     <xsl:apply-templates select="*"> 
     <xsl:sort select="@id" data-type="number"/> 
     </xsl:apply-templates> 
    </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 
+0

感謝烏爾rply ..但我沒有得到標籤內的內容Martin ... – Ramesh 2012-02-01 12:42:45

+0

它給出了一些重複的值。 – Ramesh 2012-02-01 13:08:30