2017-02-26 38 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

感谢您的帮助,一切都以某种方式进行。 –