4

xmlのファイルが3つあります

<step>
<Products>
    <Product UserTypeID="Country">
        <Name>Cyprus</Name>
        <Product UserTypeID="Resort">
            <Name>Argaka</Name>
            <Product UserTypeID="Property">
                <Name>Villa Tester</Name>
            </Product>
        </Product>
        <Product UserTypeID="Resort">
            <Name>Coral Bay</Name>
            <Product UserTypeID="Property">
                <Name>1</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>2</Name>
            </Product>
        </Product>
    </Product>
    <Product UserTypeID="Country">
        <Name>Greece</Name>
        <Product UserTypeID="Region">
            <Name>Corfu</Name>
            <Product UserTypeID="Resort">
                <Name>Aghios Stefanos</Name>
                <Product UserTypeID="Property">
                    <Name>Villa Joanna</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa Eleonas</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Kassiopi</Name>
                <Product UserTypeID="Property">
                    <Name>Villa 2</Name>
                </Product>
            </Product>
        </Product>
    </Product>
</Products>

<step>
<Products>
    <Product UserTypeID="Country">
        <Name>Cyprus</Name>
        <Product UserTypeID="Resort">
            <Name>Argaka</Name>
            <Product UserTypeID="Property">
                <Name>Villa Jaime</Name>
            </Product>
        </Product>
    </Product>
    <Product UserTypeID="Country">
        <Name>Greece</Name>
        <Product UserTypeID="Region">
            <Name>Corfu</Name>
            <Product UserTypeID="Resort">
                <Name>Acharavi</Name>
                <Product UserTypeID="Property">
                    <Name>Villa 1</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa 2</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Gouvia</Name>
                <Product UserTypeID="Property">
                    <Name>Villa De Bono</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Kassiopi</Name>
                <Product UserTypeID="Property">
                    <Name>Villa 1</Name>
                </Product>
            </Product>
        </Product>
    </Product>
</Products>

<step>
<Products>
    <Product UserTypeID="Country">
        <Name>Cyprus</Name>
        <Product UserTypeID="Resort">
            <Name>Aghia Marina</Name>
            <Product UserTypeID="Property">
                <Name>Villa Aghia Marina</Name>
            </Product>
        </Product>
        <Product UserTypeID="Resort">
            <Name>Coral Bay</Name>
            <Product UserTypeID="Property">
                <Name>Ascos Coral Villas</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>Coral Villa</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>Lella Villas</Name>
            </Product>
        </Product>
    </Product>
    <Product UserTypeID="Country">
        <Name>Greece</Name>
        <Product UserTypeID="Region">
            <Name>Corfu</Name>
            <Product UserTypeID="Resort">
                <Name>Acharavi</Name>
                <Product UserTypeID="Property">
                    <Name>Villa Angelos</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa Eleonas</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Aghios Stefanos</Name>
                <Product UserTypeID="Property">
                    <Name>Villa Joanna</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa Eleonas</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Kassiopi</Name>
                <Product UserTypeID="Property">
                    <Name>Villa Imerolia</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Test Property</Name>
                </Product>
            </Product>
        </Product>
    </Product>
</Products>

各ファイルには同じ製品(./nameによる)がありますが、サブ製品(./nameによる)が異なります。製品/名前ごとに1つの製品を含む1つのツリーに連結し、同じルールのすべてのサブ製品を含める必要があります。 1つの構造を出力できます。

xsltメソッドが見つかりました。これにより、以下のようなノードセットが作成されます。

    <xsl:variable name="step-output">
    <xsl:for-each select="/index/file">
        <xsl:copy-of select="document(.)" />
    </xsl:for-each>
</xsl:variable>
<xsl:variable name="step-products" select="exsl:node-set($step-output)//Products" />

しかし、これは、他のテンプレートを作成すると、製品/名前で3つの製品が作成されます。つまり、キプロスが3回表示されます。

誰かが私が求めていることを行う方法を知っていますか?私の結果は次のようになる必要があります

<step>
<Products>
    <Product UserTypeID="Country">
        <Name>Cyprus</Name>
        <Product UserTypeID="Resort">
            <Name>Aghia Marina</Name>
            <Product UserTypeID="Property">
                <Name>Villa Aghia Marina</Name>
            </Product>
        </Product>
        <Product UserTypeID="Resort">
            <Name>Argaka</Name>
            <Product UserTypeID="Property">
                <Name>Villa Jaime</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>Villa Tester</Name>
            </Product>
        </Product>
        <Product UserTypeID="Resort">
            <Name>Coral Bay</Name>
            <Product UserTypeID="Property">
                <Name>Ascos Coral Villas</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>Coral Villa</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>Lella Villas</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>1</Name>
            </Product>
            <Product UserTypeID="Property">
                <Name>2</Name>
            </Product>
        </Product>
    </Product>
    <Product UserTypeID="Country">
        <Name>Greece</Name>
        <Product UserTypeID="Region">
            <Name>Corfu</Name>
            <Product UserTypeID="Resort">
                <Name>Acharavi</Name>
                <Product UserTypeID="Property">
                    <Name>Villa Angelos</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa Eleonas</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa 1</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa 2</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Aghios Stefanos</Name>
                <Product UserTypeID="Property">
                    <Name>Villa Joanna</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa Eleonas</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Gouvia</Name>
                <Product UserTypeID="Property">
                    <Name>Villa De Bono</Name>
                </Product>
            </Product>
            <Product UserTypeID="Resort">
                <Name>Kassiopi</Name>
                <Product UserTypeID="Property">
                    <Name>Villa Imerolia</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Test Property</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa 1</Name>
                </Product>
                <Product UserTypeID="Property">
                    <Name>Villa 2</Name>
                </Product>
            </Product>
        </Product>
    </Product>
</Products>

4

2 に答える 2

4

これがその仕事をするはずのXSLT2.0スタイルシートです:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:output indent="yes"/>

  <xsl:template match="/">
    <step>
      <Products>
        <xsl:for-each-group select="document(index/file)/step/Products/Product" group-by="Name">
          <Product UserTypeID="{@UserTypeID}">
            <Name><xsl:value-of select="current-grouping-key()"/></Name>
            <xsl:for-each-group select="current-group()/Product" group-by="Name">
              <xsl:sort select="current-grouping-key()"/>
              <Product UserTypeID="{@UserTypeID}">
                <Name><xsl:value-of select="current-grouping-key()"/></Name>
                <xsl:for-each select="current-group()/Product">
                  <xsl:sort select="Name"/>
                  <xsl:copy-of select="."/>
                </xsl:for-each>
              </Product>
            </xsl:for-each-group>
          </Product>
        </xsl:for-each-group>
      </Products>
    </step>
  </xsl:template>

</xsl:stylesheet>

構造を持つインデックスXMLドキュメントに対して実行する必要があります

<index>
  <file>test2010020803.xml</file>
  <file>test2010020804.xml</file>
  <file>test2010020805.xml</file>
</index>

処理したい他のファイルが一覧表示されます。

XSLT 2.0スタイルシートは、.NETおよびJavaバージョンで提供されるSaxon 9で実行できるため、少なくともJava1.5または.NET2.0が使用可能であるか、インストールできる場所であればどこでも実行できます。その他のオプションは、AltovaXMLツール(Windowsのみ)とGestaltです。

XSLT 1.0に関連付けられている場合は、exsl:node-setまたは同様のサポートがある限り、次のように実行できます。

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="exsl"
  version="1.0">

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="k1" match="step/Products/Product" use="Name"/>
  <xsl:key name="k2" match="step/Products/Product/Product" use="concat(../Name, '|', Name)"/>

  <xsl:template match="/">
    <xsl:variable name="rtf">
      <xsl:copy-of select="document(index/file)/*"/>
    </xsl:variable>
    <step>
      <Products>
        <xsl:for-each select="exsl:node-set($rtf)/step/Products/Product[generate-id() = generate-id(key('k1', Name)[1])]">
          <Product UserTypeID="{@UserTypeID}">
            <xsl:copy-of select="Name"/>
            <xsl:for-each select="key('k1', Name)/Product[generate-id() = generate-id(key('k2', concat(../Name, '|', Name))[1])]">
              <xsl:sort select="Name"/>
              <Product UserTypeID="{@UserTypeID}">
                <xsl:copy-of select="Name"/>
                <xsl:for-each select="key('k2', concat(../Name, '|', Name))/Product">
                  <xsl:sort select="Name"/>
                  <xsl:copy-of select="."/>
                </xsl:for-each>
              </Product>
            </xsl:for-each>
          </Product>
        </xsl:for-each>
      </Products>
    </step>
  </xsl:template>

</xsl:stylesheet>

キーは次のようになります。

  <xsl:key name="k1" match="step/Products/Product" use="Name"/>

  <xsl:key name="k2" match="step/Products/Product/Product" use="concat(../Name, '|', Name)"/>

  <xsl:key name="k3" match="step/Products/Product/Product/Product"
                     use="concat(../../Name, '|', ../Name, '|', Name)"/>

  <xsl:key name="k4" 
           match="step/Products/Product/Product/Product/Product"
           use="concat(../../../Name, '|', ../../Name, '|', ../Name, '|', Name)"/>

  <xsl:key name="k5" 
           match="step/Products/Product/Product/Product/Product/Product"
           use="concat(../../../../Name, '|', ../../../Name, '|', ../../Name, '|', ../Name, '|', Name)"/>

  <xsl:key name="k6" 
           match="step/Products/Product/Product/Product/Product/Product/Product"
           use="concat(../../../../../Name, '|', ../../../../Name, '|', ../../../Name, '|', ../../Name, '|', ../Name, '|', Name)"/>

これはすべてフォーラムエディタに直接入力されるため、バグが発生する可能性があります。

于 2010-02-08T15:31:13.437 に答える
0

テキストを編集してファイルを作成することはできますが、保守が難しい場合があります。

最も簡単な方法は、3つのファイルすべてのXMLをオブジェクト形式に解析することです。プログラムで単一の親ノードの下にオブジェクトを追加してから、新しいXMLファイルを再生成します。

あなたの環境はこれを許容できる解決策にしますか?

于 2010-02-08T15:15:32.577 に答える