我有以下 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>