2017-06-29 5 views
0

私は以下のシナリオを持っています。私はネスト変換を一度も行っていないので、どのように起動するのか分かりません。私は次のサンプルを持っています。値が2桁の場合、これはすべての子孫の祖先になりました。入力xsltに複数の要素をネストする方法

<Items Title="Title" Icon="Icon" Description="Description"> 
    <Item Value="01" Name="Agriculture"/> 
    <Item Value="011" Name="Horticulture and Fruit Growing"/> 
    <Item Value="0111" Name="Plant Nurseries"/> 
    <Item Value="011101" Name="Bulb Propagating"/> 
    <Item Value="0112" Name="Cut Flower and Flower Seed Growing"/> 
    <Item Value="011201" Name="Display Foliage Growing"/> 

... ...値は

を継続している願望出力:

<Items Title="Title" Icon="Icon" Description="Description"> 
    <Item Name="Agriculture"> 
     <Item Value="011" Name="Horticulture and Fruit Growing"> 
      <Item Value="0111" Name="Plant Nurseries"> 
       <Item Value="011101" Name="Bulb Propagating" /> 
      </Item> 
      <Item Value="0112" Name="Cut Flower and Flower Seed Growing"> 
       <Item Value="011201" Name="Display Foliage Growing" /> 
      </Item> 
     </Item> 
    </Item> 

...私は「

veは次のxsltを使用しました:

<xsl:template match="Items"> 
     <Items Title="{@Title}" Icon="{@Icon}" Description="{@Description}"> 

      <xsl:for-each select="Item"> 
       <xsl:if test="string-length(@Value) = 2"> 
        <Item Name="{@Name}"> 
         <xsl:for-each select="ancestor::Items/Item"> 
          <xsl:if test="string-length(@Value) = 3"> 
           <Item Value="{@Value}" Name="{@Name}"> 

           </Item> 
          </xsl:if> 
         </xsl:for-each> 
        </Item> 
       </xsl:if> 
      </xsl:for-each> 

     </Items> 
    </xsl:template> 

しかし、これは4と6レベルのために働いていません。

今の結果は次のとおりです。

<Items Title="Title" Icon="Icon" Description="Description"> 
<Item Name="Agriculture"> 
    <Item Value="011" Name="Horticulture and Fruit Growing"/> 

...

答えて

1

あなたはいくつかのレベルを処理するようxsl:for-each-group group-starting-withを使用すると、それは再帰関数を記述することが最善であることを解決することができます。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mf="http://example.com/mf" 
    exclude-result-prefixes="xs mf" 
    version="2.0"> 

    <xsl:output indent="yes"/> 

    <xsl:function name="mf:group" as="element(Item)*"> 
     <xsl:param name="items" as="element(Item)*"/> 
     <xsl:param name="level" as="xs:integer"/> 
     <xsl:for-each-group select="$items" group-starting-with="Item[string-length(@Value) eq $level]"> 
      <xsl:choose> 
       <xsl:when test="self::Item[string-length(@Value) eq $level]"> 
        <xsl:copy> 
         <xsl:copy-of select="@*"/> 
         <xsl:sequence select="mf:group(current-group() except ., min((current-group() except .)/string-length(@Value)))"/> 
        </xsl:copy> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:apply-templates select="mf:group(current-group(), min(current-group()/@Value))"/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each-group> 
    </xsl:function> 

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

    <xsl:template match="Items"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:sequence select="mf:group(Item, 2)"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

ありがとうございます。それは大丈夫です。今私は関数をテストし、それから学びます。 – DanielCSD

関連する問題