I have the following XML example that I need some help on.
The sample XML has PackageID and SourcePackageID values that are required to
match other nodes to find the result I am after.
My apologies in advance for this is quite confusing for me to explain.
Please let me know if you require more information to make sense of this. 🙂
The result required is to find a MaterialPackage match for each Source element in the CompositionPackage whose Usage element is “TopLevel”.
Some results are easier to find because they are One match away. Others though require looping though more than one match to find the result.
For Example: The resulting /Name element references the PackageName of the CompositionPackage that has a matching PackageID.
However: the resulting /Source element is required to reference the SourcePackageID of the matched CompositionPackage and then use its PackageID to find the PackageName of the Material Package.
In one instance the CompositionPackage/SourcePackageID references directly to the MaterialPackage. To make it a little more difficult though if a CompositionPackage has the /Operation value of “RepeatTransfer” it will reference another CompositionPackage before linking to the MaterialPackage. Hmmm
Any help would be truly appreciated.
Sample Input 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>
Expected Output 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>
Current 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>
One way to approach this is through the use of xsl:key to look up the CompositionPackage and MaterialPackage elements, by way of a PackageID
Then, assuming you were positioned on your ‘top-level’ Source elements, you would start by selecting the relevant CompositionPackage for the package
Note the use of the mode. This will be needed because in the resultant xslt, there will be multiple templates matching CompositionPackage elements. Within the first template for it though, you would start by outputing the Book elements
For the *Source element, you would start a potentially recursive look-up. You would have another template to match CompositionPackage elements, but in this one you would check whether the package is within another CompositionPackage or a MaterialPackage. This would make use of the keys
If it was a CompositionPackage the template would recursively match itself. For the MatertialPackage though, you would simply output the package name.
Here is the full XSLT
When applied to your XSLT, the following is output