6

私はこのようなXMLを持っています

<ContractInfo  ContractNo="12345">
                <Details LastName="Goodchild">                        
                        <Filedata  FileName="File1"/>
                </Details>
</ContractInfo>

<ContractInfo  ContractNo="12345">
                <Details LastName="Goodchild">                        
                        <Filedata  FileName="File2"/>
                </Details>
</ContractInfo>

<ContractInfo  ContractNo="123456">
                <Details LastName="Goodchild">                        
                        <Filedata  FileName="File2"/>
                </Details>
</ContractInfo>

出力 XML を次のようにしたい

<ContractInfo  ContractNo="12345">
                <Details LastName="Goodchild">                        
                        <Filedata  FileName="File1"/>
                        <Filedata  FileName="File2"/>
                </Details>
</ContractInfo>

<ContractInfo  ContractNo="123456">
                <Details LastName="Goodchild">                        
                        <Filedata  FileName="File2"/>
                </Details>
</ContractInfo>

ここでは、一致する「contractNo」に関連する「FileData」を出力で結合する必要があります。この変換は XSLT で実現できますか?

前もって感謝します。

スリーニ

4

1 に答える 1

7

次の XSLT 1.0 変換は正しい結果を生成します。

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

  <xsl:output method="xml" indent="yes" />

  <xsl:key name="contract" match="ContractInfo" use="@ContractNo" />
  <xsl:key name="filedata" match="Filedata" use="../../@ContractNo" />

  <xsl:template match="ContractInfo">
    <xsl:if test="generate-id() = 
                  generate-id(key('contract', @ContractNo)[1])">
      <xsl:copy>
        <xsl:apply-templates select="key('contract', @ContractNo)/Details | @*" />
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <xsl:template match="Details">
    <xsl:if test="generate-id(..) = 
                  generate-id(key('contract', ../@ContractNo)[1])">
      <xsl:copy>
        <xsl:apply-templates select="key('filedata', ../@ContractNo) | @*" />
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <!-- copy everything else (root node, Filedata nodes and @attributes) -->
  <xsl:template match="* | @*">
    <xsl:copy>
      <xsl:apply-templates select="* | @*" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

<xsl:key>と組み合わせて を使用してgenerate-id()、一致するノード セットの最初のノードを識別し、等しいノードを効果的にグループ化することに注意してください。

<xsl:sort>内で使用することにより、順序付けされた結果を強制できます<xsl:apply-templates>。わかりやすくするために、それを含めませんでした。

私のテスト出力は次のとおりです。

<root>
  <ContractInfo ContractNo="12345">
    <Details LastName="Goodchild">
      <Filedata FileName="File1"></Filedata>
      <Filedata FileName="File2"></Filedata>
    </Details>
  </ContractInfo>
  <ContractInfo ContractNo="123456">
    <Details LastName="Goodchild">
      <Filedata FileName="File2"></Filedata>
    </Details>
  </ContractInfo>
</root>
于 2009-03-14T10:02:19.540 に答える