Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8457329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:47:03+00:00 2026-06-10T12:47:03+00:00

I have the following XML example that I need some help on. The sample

  • 0

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>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:47:05+00:00Added an answer on June 10, 2026 at 12:47 pm

    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

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

    Then, assuming you were positioned on your ‘top-level’ Source elements, you would start by selecting the relevant CompositionPackage for the package

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

    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

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

    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

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

    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

    <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>
    

    When applied to your XSLT, the following is output

    <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have the following XML chain: $xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue XML example In Some XML-files $xml->assessment->outcomes_processing->outcomes->...
I have the following XML example <?xml version=1.0?> <test> <items> <item>item 1</item> <item>item 2</item>
I have the following in an XML file: <entry> ... <link href=http://www.example.com/somelink /> ...
Guys I'm new to xml in Java. I have the following task. I need
I have a very specific deserialization need, see example below: say I have following
I need help with a question that I could not answer yet. I have
I have the following XML <xml> <ObsCont xCampo=field1> <xTexto>example1</xTexto> </ObsCont> <ObsCont xCampo=field2> <xTexto>example2</xTexto> </ObsCont>
I have the following piece of XML: <xml> <ObsCont xCampo=field1> <xTexto>example1</xTexto> </ObsCont> <ObsCont xCampo=field2>
I have following xml and I want to fetch the value of node which
I have following XML, and i wish to save its data in my SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.