0

我有以下 XML 示例,我需要一些帮助。示例 XML 具有匹配其他节点以找到我所追求的结果所需的 PackageID 和 SourcePackageID 值。
我为此提前道歉,这让我很难解释。如果您需要更多信息来理解这一点,请告诉我。:)

所需的结果是为 CompositionPackage 中其 Usage 元素为“TopLevel”的每个 Source 元素找到一个 MaterialPackage 匹配项。

有些结果更容易找到,因为它们是一场比赛。其他人虽然需要循环多个匹配项才能找到结果。

例如:生成的 /Name 元素引用具有匹配 PackageID 的 CompositionPackage 的 PackageName。但是:生成的 /Source 元素需要引用匹配的 CompositionPackage 的 SourcePackageID,然后使用其 PackageID 查找材料包的 PackageName。

在一种情况下,CompositionPackage/SourcePackageID 直接引用 MaterialPackage。如果 CompositionPackage 的 /Operation 值为“RepeatTransfer”,那么它会更困难一些,它会在链接到 MaterialPackage 之前引用另一个 CompositionPackage。嗯

任何帮助将不胜感激。

示例输入 XML:

<AAG version="1.1">
<Preface>
<ContentStorage>
  <MaterialPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
            <ComponentLength>5066</ComponentLength>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <PackageName>Tape011</PackageName>
    <PackageID>urn:MM111</PackageID>
  </MaterialPackage>
  <CompositionPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
            <SourcePackageID>urn:MM111</SourcePackageID>
            <ComponentLength>10099</ComponentLength>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <Operation>Transfer</Operation>
    <PackageName>NameABC</PackageName>
    <PackageID>urn:CC111</PackageID>
  </CompositionPackage>
  <CompositionPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
            <SourcePackageID>urn:CC333</SourcePackageID>
            <ComponentLength>222</ComponentLength>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <Operation>RepeatTransfer</Operation>
    <PackageName>NameBCD</PackageName>
    <PackageID>urn:CC222</PackageID>
  </CompositionPackage>
  <CompositionPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
            <SourcePackageID>urn:MM222</SourcePackageID>
            <ComponentLength>444</ComponentLength>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <Operation>Transfer</Operation>
    <PackageName>NameCDE</PackageName>
    <PackageID>urn:CC333</PackageID>
  </CompositionPackage>
  <CompositionPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
            <SourcePackageID>urn:MM333</SourcePackageID>
            <ComponentLength>555</ComponentLength>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <Operation>Transfer</Operation>
    <PackageName>NameDEF</PackageName>
    <PackageID>urn:CC444</PackageID>
  </CompositionPackage>
  <MaterialPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
        <ComponentLength>864</ComponentLength>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <PackageName>Tape012</PackageName>
    <PackageID>urn:MM222</PackageID>
  </MaterialPackage>
  <MaterialPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
        <ComponentLength>864</ComponentLength>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <PackageName>Tape013</PackageName>
    <PackageID>urn:MM333</PackageID>
  </MaterialPackage>
  <CompositionPackage>
    <PackageTracks>
      <TimelineTrack>
        <TrackSegment>
            <Sequence>
                <Source>
                    <SourcePackageID>urn:CC111</SourcePackageID>
                </Source>
                <Source>
                    <SourcePackageID>urn:CC222</SourcePackageID>
                </Source>
                <Unknown>urn:0000</Unknown>
                <Source>
                    <SourcePackageID>urn:CC444</SourcePackageID>
                </Source>
            </Sequence>
        </TrackSegment>
      </TimelineTrack>
    </PackageTracks>
    <Usage>TopLevel</Usage>
    <Operation>Transfer</Operation>
    <PackageName>NameXXX</PackageName>
    <PackageID>urn:CCXXX</PackageID>
  </CompositionPackage>
</ContentStorage>
</Preface>
</AAG>

预期输出 XML:

<AAG>
<Preface>
    <Book>
        <Name>NameABC</Name>
        <Source>Tape011</Source>
    </Book>
    <Book>
        <Name>NameBCD</Name>
        <Source>Tape012</Source>
    </Book>
    <Book>
        <Name>NameDEF</Name>
        <Source>Tape013</Source>
    </Book>
</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">

<AAG>
<Preface>
    <xsl:for-each select="/AAG/Preface/ContentStorage/CompositionPackage/PackageTracks/TimelineTrack/TrackSegment/Sequence[../../../../Usage='TopLevel']">

    <xsl:choose>
        <xsl:when test = "self::Source">
            <Book>
                <Name><xsl:value-of select="../../../../*[self::CompositionPackage or self::MaterialPackage]
                                                            [PackageID=current()
                                                            /SourcePackageID]/PackageName"/></Name>

                <!-- cant work out how to do this bit 
                                    need to find MaterialPackage/PackageName from a CompositionPackage that in turn references another CompositionPackage that in turn references the MaterialPackage. -->
                                    <xsl:choose>
                <xsl:when test = "CompositionPackage[Operation='RepeatTransfer']">
                <Source><xsl:value-of select="../../../../*[self::CompositionPackage or self::MaterialPackage]
                                                            [PackageID=current()
                                                            /SourcePackageID]/PackageName"/></Source>
                </xsl:when>
                <xsl:otherwise>
                <Source><xsl:value-of select="../../../../*[self::CompositionPackage or self::MaterialPackage]
                                                            [PackageID=current()
                                                            /SourcePackageID]/PackageName"/></Source>
                </xsl:otherwise>
                </xsl:choose>
            </Book>
        </xsl:when>

    </xsl:choose>
    </xsl:for-each>

</Preface>
</AAG>

</xsl:template>

4

1 回答 1

1

解决此问题的一种方法是使用xsl:key通过 PackageID查找CompositionPackageMaterialPackage元素

<xsl:key name="CompositionPackage" match="CompositionPackage" use="PackageID"/>
<xsl:key name="MaterialPackage" match="MaterialPackage" use="PackageID"/>

然后,假设您位于“顶级” Source元素上,您将首先为包选择相关的CompositionPackage

<xsl:apply-templates select="key('CompositionPackage', SourcePackageID)" mode="Source" />

注意模式的使用。这将是必需的,因为在生成的 xslt 中,将有多个模板匹配CompositionPackage元素。不过,在它的第一个模板中,您将从输出Book元素开始

<xsl:template match="CompositionPackage" mode="Source">
   <Book>
      <Name><xsl:value-of select="PackageName" /></Name>
      <Source><xsl:apply-templates select="." /></Source>
   </Book>
</xsl:template>

对于*Source元素,您将开始潜在的递归查找。您将有另一个模板来匹配CompositionPackage元素,但在这个模板中,您将检查包是否在另一个CompositionPackageMaterialPackage中。这将利用密钥

<xsl:template match="CompositionPackage">
  <xsl:apply-templates select="
    key('MaterialPackage',PackageTracks/TimelineTrack/TrackSegment/SourcePackageID)
    |key('CompositionPackage', PackageTracks/TimelineTrack/TrackSegment/SourcePackageID)"/>
</xsl:template>

如果它是CompositionPackage,则模板将递归匹配自身。但是,对于MaterialPackage,您只需输出包名称。

这是完整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>
   <xsl:key name="CompositionPackage" match="CompositionPackage" use="PackageID"/>
   <xsl:key name="MaterialPackage" match="MaterialPackage" use="PackageID"/>

   <xsl:template match="/AAG">
      <AAG>
         <Preface>
            <xsl:apply-templates select="Preface/ContentStorage/CompositionPackage[Usage='TopLevel']/PackageTracks/TimelineTrack/TrackSegment/Sequence/Source"/>
         </Preface>
      </AAG>
   </xsl:template>

   <xsl:template match="Source">
      <xsl:apply-templates select="key('CompositionPackage', SourcePackageID)" mode="Source" />
   </xsl:template>

   <xsl:template match="CompositionPackage" mode="Source">
      <Book>
         <Name><xsl:value-of select="PackageName" /></Name>
         <Source>
            <xsl:apply-templates select="." />
         </Source>
      </Book>
   </xsl:template>

   <xsl:template match="CompositionPackage">
      <xsl:apply-templates select="key('MaterialPackage', PackageTracks/TimelineTrack/TrackSegment/SourcePackageID)|key('CompositionPackage', PackageTracks/TimelineTrack/TrackSegment/SourcePackageID)"/>
   </xsl:template>

   <xsl:template match="MaterialPackage">
      <xsl:value-of select="PackageName" />
   </xsl:template>   

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

当应用于您的 XSLT 时,将输出以下内容

<AAG>
   <Preface>
      <Book>
         <Name>NameABC</Name>
         <Source>Tape011</Source>
      </Book>
      <Book>
         <Name>NameBCD</Name>
         <Source>Tape012</Source>
      </Book>
      <Book>
         <Name>NameDEF</Name>
         <Source>Tape013</Source>
      </Book>
   </Preface>
</AAG>
于 2012-08-25T09:34:14.507 回答