2015-10-14 112 views
0

我有這樣一個XML文件,XSLT - 應用模板到子節點和文本()節點

<content> 
    <ol> 
     <li> List<span style="color: rgb(255,0,255);">The scopeeeee of</span>this project is 
      <ol> 
       <li>nested list1</li> 
       <li>nested <span style="color: rgb(255,0,255);">The scope of this project is</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> 
</content> 

我需要使用XSLT

預期產出上面的XML轉換到下面的XML,

<content> 
    <orderedList> 
     <liItem> 
      <para>List<c type="color: rgb(255,0,255);">The scopeeeee of this project is to:</c>jkhsdlkjfh</para> 
     </liItem> 
     <orderedList> 
      <liItem> 
       <para>nested list1</para> 
      </liItem> 
      <liItem> 
       <para>nested <c type="color: rgb(255,0,255);">The scope of this project is</c>list1</para> 
      </liItem> 
      <liItem> 
       <para>nested list1</para> 
      </liItem> 
      <liItem> 
       <para>nested list1</para> 
      </liItem> 
      <liItem> 
       <para>nested list1</para> 
      </liItem> 
     </orderedList> 

     <liItem> 
      <para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para> 
     </liItem> 
     <liItem> 
      <para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para> 
     </liItem> 
    </orderedList> 
</content> 

如您所見,li項中的文本內容和跨度節點必須在輸出中包含<para>節點。

我已經寫了下面的XSL來得到這個輸出,

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

    <xsl:template match="li[not(descendant::ol)]" > 
     <liItem> 
      <para> 
       <xsl:apply-templates /> 
      </para> 
     </liItem> 
    </xsl:template> 

    <xsl:template match="li[descendant::ol]" > 
     <liItem> 
      <para> 
       <xsl:apply-templates select="node()[parent::li][following-sibling::ol]"/> 
      </para> 
     </liItem> 
     <xsl:apply-templates/> 
    </xsl:template> 

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

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

以上的xsl唯一的問題是來的時候它有<ol>內的另一個<ol>項目。我正在努力尋找一種方法,將<para>節點添加到位於<li><ol>節點之間的文本內容。

電流輸出如下,

<content> 
    <orderedList> 
     <liItem> 
      <para> List<c type="color: rgb(255,0,255);">The scopeeeee of</c>this project is</para> 
     </liItem> List<c type="color: rgb(255,0,255);">The scopeeeee of</c>this project is 
     <orderedList> 
      <liItem><para>nested list1</para></liItem> 
      <liItem><para>nested <c type="color: rgb(255,0,255);">The scope of this project is</c>list1</para></liItem> 
      <liItem><para>nested list1</para></liItem> 
      <liItem><para>nested list1</para></liItem> 
      <liItem><para>nested list1</para></liItem> 
     </orderedList> 
     <liItem><para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para></liItem> 
     <liItem><para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para></liItem> 
    </orderedList> 
</content> 

任何人都可以建議我一個辦法,我怎麼能修改我的代碼,以獲得預期輸出..

回答

4

如果ol元素總是要來後,在li元素的文字,你可以只更改模板這個

<xsl:template match="li[descendant::ol]" > 
    <liItem> 
     <para> 
      <xsl:apply-templates select="node()[following-sibling::ol]"/> 
     </para> 
    </liItem> 
    <xsl:apply-templates select="ol"/> 
</xsl:template> 

或者,也許這一點,如果你其實想個Ëol元素liItem內,但只是沒有在para

<xsl:template match="li[descendant::ol]" > 
    <liItem> 
     <para> 
      <xsl:apply-templates select="node()[following-sibling::ol]"/> 
     </para> 
     <xsl:apply-templates select="ol"/> 
    </liItem> 
</xsl:template> 

在這兩種情況下,你可以將二者結合起來的模板匹配li成一個單一的模板;像這樣:

<xsl:template match="li" > 
    <liItem> 
     <para> 
      <xsl:apply-templates select="node() except ol"/> 
     </para> 
     <xsl:apply-templates select="ol"/> 
    </liItem> 
</xsl:template> 

或者,如果你想要一個列表項目中有多個應付ol元素,或者如果你有ol元素之後的文字也一樣,你可以利用的xsl:for-each-group

<xsl:template match="li" > 
    <liItem> 
     <xsl:for-each-group select="node()" group-adjacent="boolean(self::ol)"> 
      <xsl:choose> 
       <xsl:when test="current-grouping-key() or not(normalize-space())"> 
        <xsl:apply-templates select="current-group() "/> 
       </xsl:when> 
       <xsl:otherwise> 
        <para> 
         <xsl:apply-templates select="current-group() "/> 
        </para> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each-group> 
    </liItem> 
</xsl:template>