I have an xml payload
<Root>
<Header>
<Name>B</Name>
</Header>
<Item>
<Id>10</Id>
<Description>Item10</Description>
</Item>
<Item>
<Id>20</Id>
<Description>Item20</Description>
</Item>
<Package>
<Id>A</Id>
</Package>
<Package>
<Id>B</Id>
</Package>
<Package>
<Id>C</Id>
</Package>
</Root>
For each Package, I am trying to get output of the entire message of all ancestors of the current Package upto its root, but not its following sibling nodes (other packages).
Desired Output:
`
<Root>
<Header>
<Name>B</Name>
</Header>
<Item>
<Id>10</Id>
<Description>Item10</Description>
</Item>
<Item>
<Id>20</Id>
<Description>Item20</Description>
</Item>
<Package>
<Id>A</Id>
</Package>
</Root>
<Root>
<Header>
<Name>B</Name>
</Header>
<Item>
<Id>10</Id>
<Description>Item10</Description>
</Item>
<Item>
<Id>20</Id>
<Description>Item20</Description>
</Item>
<Package>
<Id>B</Id>
</Package>
</Root>
<Root>
<Header>
<Name>B</Name>
</Header>
<Item>
<Id>10</Id>
<Description>Item10</Description>
</Item>
<Item>
<Id>20</Id>
<Description>Item20</Description>
</Item>
<Package>
<Id>C</Id>
</Package>
</Root>
`
But I am getting 3 messages including all packages in each.
Here is my code
<?xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<Messages>
<xsl:for-each select="//Package">
<xsl:copy-of select="/*[not(following-sibling::*)]" />
</xsl:for-each>
</Messages>
</xsl:template>
What I am doing wrong? Thank you for your advice.
To get the result you show (not the result you describe), you could do simply:
XSLT 1.0
But do note that this result is an XML fragment, not a well-formed XML document, because it lacks a single root element.