arfo Posted April 26, 2012 Share Posted April 26, 2012 I have a XML file where some of the elements appear in the incorrect place. In the example the attribute @ParentDFT tells which parent DESIGN-FUNCTION-TYPE a FUNCTION-FLOW-PORT belongs to. I want to perform transformation so that "Visiting" is moved from the "Host" to the "ChildMissing" parent. <ELEMENTS> <DESIGN-FUNCTION-TYPE UUID="DFT101"> <SHORT-NAME>Host</SHORT-NAME> <PORTS> <FUNCTION-FLOW-PORT ParentDFT="DFT101" UUID="FFP101-1"> <SHORT-NAME>AtHome</SHORT-NAME> <DIRECTION>OUT</DIRECTION> </FUNCTION-FLOW-PORT> <FUNCTION-FLOW-PORT ParentDFT="DFT102" UUID="FFP102-1"> <SHORT-NAME>Visiting</SHORT-NAME> <DIRECTION>OUT</DIRECTION> </FUNCTION-FLOW-PORT> </PORTS> </DESIGN-FUNCTION-TYPE> <DESIGN-FUNCTION-TYPE UUID="DFT102"> <SHORT-NAME>ChildMissing</SHORT-NAME> <PORTS> </PORTS> </DESIGN-FUNCTION-TYPE></ELEMENTS> I have partly managed to achieve the goal with the following XSLT: [<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict" version="1.0"><!-- <xsl:strip-space elements="doc chapter section"/> --><xsl:output method="xml" indent="yes" encoding="UTF-8"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy></xsl:template> <xsl:template match="DESIGN-FUNCTION-TYPE"> xsl:variable name="DFTuuid" select="@UUID"/> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="SHORT-NAME"/> <xsl:apply-templates select="//FUNCTION-FLOW-PORT[@ParentDFT=$DFTuuid]"/> </xsl:copy></xsl:template> </xsl:stylesheet> Problem is that the PORTS element is lost.If I add <xsl:apply-templates select="PORTS"/> the complete PORTS element appears (including all the FUNCTION-FLOW-PORTS) How do I get the PORTS element back in the correct place? 1 Link to comment Share on other sites More sharing options...
DVezina Posted April 27, 2012 Share Posted April 27, 2012 This is one of many ways: <xsl:template match="DESIGN-FUNCTION-TYPE"> <xsl:variable name="DFTuuid" select="@UUID"/> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="SHORT-NAME"/> <xsl:element name="PORTS"> <xsl:apply-templates select="PORTS/FUNCTION-FLOW-PORT[@ParentDFT=$DFTuuid]"/> </xsl:element> </xsl:copy></xsl:template> 1 Link to comment Share on other sites More sharing options...
arfo Posted April 28, 2012 Author Share Posted April 28, 2012 tx a lot Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now