2017-02-26 95 views
0

我很困惑如何爲下面的代碼編寫遞歸循環。嘗試逗號分離,但看起來像循環只會工作。任何解決方案,想法都會有幫助?謝謝我使用XML版本1.下面只是一個例子。如何爲逗號分隔的節點編寫遞歸循環

我的XML數據源:

<Groceries> 
    <fruit>Apple,Banana,Peach,Lemon</fruit> 
</Groceries> 

我找一個XSLT得到下面的輸出。
所以我期望的XML輸出應該是這樣的:

<Foods> 
     <Food raw="Lemon" cat="Fruit" val="Lemon"/> 
     <Food raw="Apple" cat="Fruit" val="Apple"/> 
     <Food raw="Peach" cat="Fruit" val="Peach"/> 
     <Food raw="Banana" cat="Fruit" val="Banana"/> 
</Foods> 

我試圖創建一個XSLT的解決辦法是:

<xsl:element name="Fruit"> 
    <xsl:attribute name="raw"> 
    <xsl:value-of select="substring-before(fruit,',')"/> 
    </xsl:attribute> 
    <xsl:attribute name="cat">Fruit</xsl:attribute> 
    <xsl:attribute name="val"> 
    <xsl:value-of select="substring-before(fruit,',')"/> 
    </xsl:attribute> 
</xsl:element> 
<xsl:text>&#xa;</xsl:text> 
<xsl:element name="Fruit"> 
    <xsl:attribute name="raw"> 
    <xsl:value-of select="substring-after(fruit,',')"/> 
    </xsl:attribute> 
    <xsl:attribute name="cat">Fruit</xsl:attribute> 
    <xsl:attribute name="val"> 
    <xsl:value-of select="substring- after(fruit,',')"/> 
    </xsl:attribute> 
</xsl:element> 
<xsl:text>&#xa;</xsl:text> 

感謝

+1

您應該添加您的嘗試解決方案。 –

+1

** 1。**「晚餐」和「車」的價值從哪裏來? - ** 2。**請發佈完整和準確的預期產出;當然,你不需要一個「遞歸循環」來提取逗號分隔列表的第一個值? - ** 3。**請說明您將使用哪個XSLT處理器。 –

+0

version =「1.0」encoding =「UTF-8」 –

回答

0

這XSLT的1.0遞歸模板達到預期結果:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/Groceries"> 
     <Foods> 
     <xsl:call-template name="fruit">   <!-- call the recursive template --> 
      <xsl:with-param name="txt" select="concat(fruit/text(),',')" /> <!-- add a leading ',' to make the recursive template behave properly --> 
     </xsl:call-template> 
     </Foods> 
    </xsl:template> 

    <xsl:template name="fruit"> 
     <xsl:param name="txt" /> 
     <xsl:if test="$txt != ''"> 
     <xsl:variable name="x" select="normalize-space(substring-before($txt,','))"/> 
     <Food raw="{$x}" cat="Fruit" val="{$x}" /> <!-- add 'Food' element with attributes --> 
     <xsl:call-template name="fruit">   <!-- call the recursive template with the rest of the comma-separated-string --> 
      <xsl:with-param name="txt" select="normalize-space(substring-after($txt,','))" /> 
     </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

它的輸出是des IRED:

<?xml version="1.0"?> 
<Foods> 
    <Food raw="Apple" cat="Fruit" val="Apple"/> 
    <Food raw="Banana" cat="Fruit" val="Banana"/> 
    <Food raw="Peach" cat="Fruit" val="Peach"/> 
    <Food raw="Lemon" cat="Fruit" val="Lemon"/> 
</Foods> 
0

您建立通過給template元素的名稱,有它調用自身,如果某些條件得到滿足,並使用call-template元素調用它的內容遞歸模板。因爲它不包含任何templatecall-template元素並且不是有效的XSLT樣式表,所以您的嘗試根本沒有嘗試。

該代碼將遞歸地產生所描述的輸出,包括所描述的輸出元素的看似隨機順序。

<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <output media-type="application/xml"/> 
    <template name="parse.CSV.string"> 
     <param name="CSV.string.element"/> 
     <param name="CSV.string" select="string($CSV.string.element/child::text())"/> 
     <param name="required.item.string" select="string($CSV.string.element/child::text())"/> 
     <variable name="item.string"> 
      <choose> 
       <when test="contains($CSV.string, ',')"> 
        <value-of select="substring-before($CSV.string, ',')"/> 
       </when> 
       <otherwise> 
        <value-of select="$CSV.string"/> 
       </otherwise> 
      </choose> 
     </variable> 
     <choose> 
      <when test="$item.string = $required.item.string"> 
       <element name="Food"> 
        <attribute name="raw"> 
         <value-of select="$item.string"/> 
        </attribute> 
        <attribute name="cat">Fruit</attribute> 
        <attribute name="val"> 
         <value-of select="$item.string"/> 
        </attribute> 
       </element> 
      </when> 
      <when test="contains($CSV.string, ',')"> 
       <call-template name="parse.CSV.string"> 
        <with-param name="CSV.string.element" select="$CSV.string.element"/> 
        <with-param name="CSV.string" select="substring-after($CSV.string, ',')"/> 
        <with-param name="required.item.string" select="$required.item.string"/> 
       </call-template> 
      </when> 
     </choose> 
    </template> 
    <template match="/child::Groceries"> 
     <element name="Foods"> 
      <call-template name="parse.CSV.string"> 
       <with-param name="CSV.string.element" select="child::fruit"/> 
       <with-param name="required.item.string" select="'Lemon'"/> 
      </call-template> 
      <call-template name="parse.CSV.string"> 
       <with-param name="CSV.string.element" select="child::fruit"/> 
       <with-param name="required.item.string" select="'Apple'"/> 
      </call-template> 
      <call-template name="parse.CSV.string"> 
       <with-param name="CSV.string.element" select="child::fruit"/> 
       <with-param name="required.item.string" select="'Peach'"/> 
      </call-template> 
      <call-template name="parse.CSV.string"> 
       <with-param name="CSV.string.element" select="child::fruit"/> 
       <with-param name="required.item.string" select="'Banana'"/> 
      </call-template> 
     </element> 
    </template> 
</transform> 
+0

感謝您的幫助,一切都以某種方式進行。 –