Jump to content

XSLT transformation - rearranging elements


arfo

Recommended Posts

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?

  • Like 1
Link to comment
Share on other sites

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>

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...