-1

2 つのノードの通りをマージする必要があります。

  • 親ノード: 都市 NEW YORK
  • 同じ方法: 変更
  • 同じID: 0

属性値をマージする必要があります (この投稿の最後にある出力ファイルを参照してください)

入力ファイルは次のとおりです。

<country>
<state id="NEW JERSEY">
    <city id="NEW YORK">
     <district id="BRONX" method="modify">

        <street id="0" method="modify">
           <attributes>
              <temperature>98</temperature>
              <altitude>1300</altitude>
           </attributes>
        </street>

        <dadada id="99" method="modify" />

        <street id="0" method="modify">
           <attributes>
              <temperature>80</temperature>
              <streetnumber> 67 </streetnumber>
           </attributes>
        </street>

        <dididi id="432" method="modify" />

     </district>


  </city>

</state>

期待される出力:

<country>
<state id="NEW JERSEY">
    <city id="NEW YORK">
     <district id="BRONX" method="modify">

        <street id="0" method="modify">
           <attributes>
              <temperature>80</temperature>
              <altitude>1300</altitude>
              <streetnumber> 67 </streetnumber>
           </attributes>
        </street>

        <dadada id="99" method="modify" />

        <dididi id="432" method="modify" />

     </district>

  </city>

</state>
</country>

助けてください、私は XSLT を始めたばかりです

4

1 に答える 1

1

あなたが XSLT 2.0 に興味を持っていると仮定したのは、それがあなたの質問にタグを付けた方法だからです。XSLT 1.0 に相当するものが必要な場合はお知らせください。この XSLT 2.0 スタイルシートはうまくいくはずです...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

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

<xsl:template match="*[street]">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
   <xsl:for-each-group select="street" group-by="@method">
    <xsl:apply-templates select="current-group()[1]" />
   </xsl:for-each-group>
   <xsl:apply-templates select="node()[not(self::street)]"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="street/attributes">
 <xsl:copy>
  <xsl:apply-templates select="@*"/>
   <xsl:variable name="grouped-method" select="../@method" />  
   <xsl:for-each-group select="../../street[@method=$grouped-method]/attributes/*" group-by="name()">
    <xsl:apply-templates select="current-group()[1]" />
   </xsl:for-each-group>    
  <xsl:apply-templates select="comment()|processing-instruction()"/>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet> 

説明

2 番目のテンプレートは、通りの親である要素に一致し、共通の方法で子の通りをグループ化します。グループごとに、グループ内の最初の道路のみがコピーされます。残りはドロップされます。

グループのこの最初のストリートが 3 番目のテンプレートで「属性」ノード プロセスを持っている場合、同じグループのすべての属性をマージします。'attributes' は、XML ドキュメント内の不適切な要素名である可能性があります。このグループ化は、同じ通りの親 (ブロンクス地区) を持つすべての関連する通りのすべての「属性」子ノードを調べ、要素名でグループ化することによって実現されます。そのようなグループに複数の要素がある場合は、最初の要素から値を取得してください。

通りの属性は「親」ノード (ブロンクス) によってマージされますが、都市レベルではマージされないため、これがまさにあなたが望むものかどうかはわかりません。これは、質問のあいまいさを反映しています。サンプル日付の通りの「親」ノードは、市ではなく地区です。これが間違っていて、都市レベルでグループ化する必要がある場合は、質問を明確にして更新してください。

于 2012-07-11T09:35:39.867 に答える