XSL - 使用当前上下文的值查找另一个节点值

时间:2012-08-17 02:04:37

标签: xml xslt

我有一个XML,它使用PackageID值来引用文档中的其他节点。 我想使用当前节点的结果,然后在文档中找到包含匹配的PackageID的另一个节点的值。 在这种情况下,我想获取SourcePackage / PackageName或MaterialPackage / PackageName或另一个CompositionPackage的值,该值具有与CompositionPackage / PackageTracks / TimelineTrack / TrackSegment / SourcePackageID中找到的PackageID值匹配的。

示例XML:

<AAG version="1.1">
<Preface>
  <ContentStorage>
      <SourcePackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <ComponentLength>5066</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>Name001</PackageName>
        <PackageID>urn:44f70006</PackageID>
      </SourcePackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <SourcePackageID>urn:44f70006</SourcePackageID>
                <ComponentLength>10099</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameABC</PackageName>
        <PackageID>urn:ce1904fd</PackageID>
      </CompositionPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <SourcePackageID>urn:00f70009</SourcePackageID>
                <ComponentLength>222</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameABC</PackageName>
        <PackageID>urn:ff1603fd</PackageID>
      </CompositionPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <SourcePackageID>urn:11f80008</SourcePackageID>
                <ComponentLength>444</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameBCD</PackageName>
        <PackageID>urn:gg1702fj</PackageID>
      </CompositionPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <SourcePackageID>urn:22f90009</SourcePackageID>
                <ComponentLength>555</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameXYZ</PackageName>
        <PackageID>urn:11f80008</PackageID>
      </CompositionPackage>
      <MaterialPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
            <ComponentLength>864</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>Name002</PackageName>
        <PackageID>urn:00f70009</PackageID>
      </MaterialPackage>
  </ContentStorage>
</Preface>
</AAG>

示例XSL:

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

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

 <xsl:template match = "AAG">
 <Master>
 <xsl:for-each select="AAG/Preface/ContentStorage">
 <newnode>
   <value1><xsl:value-of select ="CompositionPackage/PackageTracks/TimelineTrack/TrackSegment/ComponentLength"/></value1>
   <value2><xsl:value-of select ="CompositionPackage/PackageTracks/TimelineTrack/TrackSegment/SourcePackageID"/></value2>
   <value3><xsl:value-of select ="CompositionPackage/PackageTracks/TimelineTrack/TrackSegment[SourcePackageID=current()/MaterialPackage/PackageName"/></value3>
 </newnode>
 </xsl:for-each>
 </Master>
 </xsl:template>
 </xsl:stylesheet>

预期的XML:

<Master>
<newnode>
  <Value1>1099</Value1>
  <Value2>urn:44f70006</Value2>
  <Value3>Name001</Value3>
</newnode>
<newnode>
  <Value1>222</Value1>
  <Value2>urn:00f70009</Value2>
  <Value3>Name002</Value3>
</newnode>
<newnode>
  <Value1>444</Value1>
  <Value2>urn:11f80008</Value2>
  <Value3>NameXYZ</Value3>
</newnode>
<newnode>
  <Value1>555</Value1>
  <Value2>urn:22f90009</Value2>
  <Value3></Value3>
</newnode>
</Master>

My Current XSL正在返回&#34;&#34;对于所有value3元素。

1 个答案:

答案 0 :(得分:1)

提供的转化有几处错误

下面的转换是提供错误更正的

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

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

  <xsl:template match = "AAG">
   <Master>
     <xsl:for-each select="Preface/ContentStorage/CompositionPackage">
         <newnode>
           <value1>
              <xsl:value-of select=
                "PackageTracks/TimelineTrack/TrackSegment/ComponentLength"/>
           </value1>
           <value2>
             <xsl:value-of select=
              "PackageTracks/TimelineTrack/TrackSegment/SourcePackageID"/>
       </value2>
           <value3>
             <xsl:value-of select=
              "../*[self::SourcePackage or self::MaterialPackage]
                                [PackageID
                                =
                                current()
                                 /PackageTracks/TimelineTrack/TrackSegment/SourcePackageID
                                ]
                                /PackageName
                 "/>
           </value3>
         </newnode>
   </xsl:for-each>
  </Master>
  </xsl:template>
 </xsl:stylesheet>

在提供的XML文档上应用此转换时

<AAG version="1.1">
    <Preface>
        <ContentStorage>
            <SourcePackage>
                <PackageTracks>
                    <TimelineTrack>
                        <TrackSegment>
                            <ComponentLength>5066</ComponentLength>
                        </TrackSegment>
                    </TimelineTrack>
                </PackageTracks>
                <PackageName>Name001</PackageName>
                <PackageID>urn:44f70006</PackageID>
            </SourcePackage>
            <CompositionPackage>
                <PackageTracks>
                    <TimelineTrack>
                        <TrackSegment>
                            <SourcePackageID>urn:44f70006</SourcePackageID>
                            <ComponentLength>10099</ComponentLength>
                        </TrackSegment>
                    </TimelineTrack>
                </PackageTracks>
                <PackageName>NameABC</PackageName>
                <PackageID>urn:ce1904fd</PackageID>
            </CompositionPackage>
            <CompositionPackage>
                <PackageTracks>
                    <TimelineTrack>
                        <TrackSegment>
                            <SourcePackageID>urn:00f70009</SourcePackageID>
                            <ComponentLength>222</ComponentLength>
                        </TrackSegment>
                    </TimelineTrack>
                </PackageTracks>
                <PackageName>NameABC</PackageName>
                <PackageID>urn:ff1603fd</PackageID>
            </CompositionPackage>
            <MaterialPackage>
                <PackageTracks>
                    <TimelineTrack>
                        <TrackSegment>
                            <ComponentLength>864</ComponentLength>
                        </TrackSegment>
                    </TimelineTrack>
                </PackageTracks>
                <PackageName>Name002</PackageName>
                <PackageID>urn:00f70009</PackageID>
            </MaterialPackage>
        </ContentStorage>
    </Preface>
</AAG>

产生了想要的正确结果

<Master>
   <newnode>
      <value1>10099</value1>
      <value2>urn:44f70006</value2>
      <value3>Name001</value3>
   </newnode>
   <newnode>
      <value1>222</value1>
      <value2>urn:00f70009</value2>
      <value3>Name002</value3>
   </newnode>
</Master>