Wednesday, 15 June 2011

xml - XSLT copy starting from certain node -



xml - XSLT copy starting from certain node -

i want move part of xml file node using xslt. want move starting node. best explain through example.

this input xml (it's sample)

<messages> <message> <header> <name>message content 1</name> </header> <info1> <description>blabla</description> </info1> <info2> <name>test</name> <description>blabla</description> </info2> ... possible more nodes (with random names) <header> <name>message content summary</name> </header> <info1> <total>blablabla</total> </info1> ... possible more nodes (with random names) </message> </messages>

i next output:

<messages> <message> <header> <name>message content 1</name> </header> <info1> <description>blabla</description> </info1> <info2> <name>test</name> <description>blabla</description> </info2> ... possible more nodes (with random names) </message> <messagesummary> <header> <name>message content summary</name> </header> <info1> <total>blablabla</total> </info1> ... possible more nodes (with random names) </messagesummary> </messages>

so want move starting lastly occurance of node "header" seperate node "messagesummary".

is there way can accomplish using xslt 1.0? help appreciated.

thanks.

so when you're @ context of input message element, want set element has @ to the lowest degree 1 header after output message , else within messagesummary. can distinguish these 2 cases *[following-sibling::header] , *[not(following-sibling::header)], if start standard identity transformation end with

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:strip-space elements="*" /> <xsl:output indent="yes" /> <!-- identity template - copies as-is unless overridden --> <xsl:template match="@*|node()"> <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy> </xsl:template> <xsl:template match="message"> <xsl:copy> <xsl:apply-templates select="*[following-sibling::header]" /> </xsl:copy> <messagesummary> <xsl:apply-templates select="*[not(following-sibling::header)]" /> </messagesummary> </xsl:template> </xsl:stylesheet>

if have other elements named message in "random names" section may wish create template more specific, e.g.

<xsl:template match="messages/message">

so catches elements want.

xml xslt-1.0

No comments:

Post a Comment