2015-10-05 79 views
0

我在做xml轉換的html。在HTML我有臺這樣的,覆蓋來自新節點的現有節點的文本()節點 - XSLT

<doc> 
<ol> 
    <li> 
     <p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p> 
     <ol> 
      <li>nested list1</li> 
      <li>nested <span style="color: rgb(255,0,255);">The scope of this project is to:</span> list1</li> 
      <li>nested list1</li> 
      <li>nested list1</li> 
      <li>nested list1</li> 
     </ol> 
    </li> 
    <li> 
     <p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p> 
    </li> 
    <li> 
     <p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p> 
    </li> 
</ol> 

在這裏,我需要的HTML元素的名稱轉換爲某些XML元素的名稱,並應包含<para>元素的文本內容。

所以我的輸出應該是,

<doc> 
    <oli> 
     <listitem><para>List<style type="color: rgb(255,0,255);">The scope of this project is to:</style></para> 
      <oli> 
       <listitem><para>nested list1</para></listitem> 
       <listitem><para>nested <style type="color: rgb(255,0,255);">The scope of this project is to:</style> list1</para></listitem> 
       <listitem><para>nested list1</para></listitem> 
       <listitem><para>nested list1</para></listitem> 
       <listitem><para>nested list1</para></listitem> 
      </oli> 
     </listitem> 
     <listitem><para> 
      List<style type="color: rgb(255,0,255);">The scope of this project is to:</style> 
     </para></listitem> 
     <listitem><para> 
      List<style type="color: rgb(255,0,255);">The scope of this project is to:</style> 
     </para></listitem> 
    </oli> 
</doc> 

我已經寫了下面的XSL轉換的HTML到XML,

<xsl:template match="ol"> 
     <oli> 
      <xsl:apply-templates/>   
     </oli> 
    </xsl:template> 

    <xsl:template match="li"> 
     <listitem> 
      <para> 
       <xsl:apply-templates/> 
      </para> 
     </listitem> 
    </xsl:template> 

    <xsl:template match="span"> 
     <style type="{@style}"> 
      <xsl:apply-templates/> 
     </style> 
    </xsl:template> 

    <xsl:template match="p"> 
     <para> 
      <xsl:apply-templates/> 
     </para> 
    </xsl:template> 

    <xsl:template match="li/p"> 
     <xsl:apply-templates/> 
    </xsl:template> 

我的問題是,當ul包含嵌套的列表中,有li文本包含嵌套列表未正確覆蓋<para>元素。

在我得到的嵌套列表上面的例子currunt輸出

是follwos,

<listitem><para> 
      List<style type="color: rgb(255,0,255);">The scope of this project is to:</style> 
      <oli> 
       <listitem><para>nested list1</para></listitem> 
       <listitem><para>nested <style type="color: rgb(255,0,255);">The scope of this project is to:</style> list1</para></listitem> 
       <listitem><para>nested list1</para></listitem> 
       <listitem><para>nested list1</para></listitem> 
       <listitem><para>nested list1</para></listitem> 
      </oli> 
     </para></listitem> 

如此簡單,而不是

<listitem><para> 
       List<style type="color: rgb(255,0,255);">The scope of this project is to:</style> 
       <oli> 
       ........ 
       </oli> 
      </para> 
</listitem> 

我需要,

<listitem><para> 
      List<style type="color: rgb(255,0,255);">The scope of this project is to:</style> 
      </para> 
        <oli> 
        ........ 
        </oli> 

    </listitem> 

我試圖覆蓋text()內容li元素由<para>但是然後<span>元素不會輸出。

任何人都可以提出一個方法,我怎麼能做到這一點..

回答

1

我只想改變模板:

<xsl:template match="li/p"> 
    <xsl:apply-templates/> 
</xsl:template> 

到:

<xsl:template match="li[p]"> 
    <listitem> 
    <xsl:apply-templates/> 
    </listitem> 
</xsl:template> 

此模板將匹配<li>那包含一個<p>。在這種情況下,您不必添加一些<para>。對於包含一些<p><li>,另一個模板將匹配,並且它確實輸出所需的<para>

或者,你也可以做這樣的事情:

  • 丟棄模板<xsl:template match="li/p">
  • 修改模板<xsl:template match="li">喜歡如下:

    <xsl:template match="li"> 
        <xsl:choose> 
        <xsl:when test="p"> 
         <listitem> 
         <xsl:apply-templates/> 
         </listitem> 
        </xsl:when> 
        <xsl:otherwise> 
         <listitem> 
         <para> 
          <xsl:apply-templates/> 
         </para> 
         </listitem> 
        </xsl:otherwise> 
        </xsl:choose> 
    </xsl:template>