Jump to content

xsl:sort difficulty


arfo

Recommended Posts

I want to transform the following XML snippet <hosts> <host> <port name="Switch A.Port3" speed="100"/> <port name="Switch A.Port1" speed="100"/> <port name="Switch A.Port4" speed="100"/> <port name="Switch A.Port2" speed="100"/> </host> </hosts> <switches> <switch modelID="1" name="Switch A"> <port name="Switch A.Port3" speed="100"/> <port name="Switch A.Port1" speed="100"/> <port name="Switch A.Port4" speed="100"/> <port name="Switch A.Port2" speed="100"/> </switch> </switches> into <hosts> <host/></hosts><switches> <switch modelID="1" name="Switch A"> <port name="Switch A.Port1" speed="100"/> <port name="Switch A.Port2" speed="100"/> <port name="Switch A.Port3" speed="100"/> <port name="Switch A.Port4" speed="100"/> </switch></switches> I have written this XSLT which takes care of the unwanted <port>s, but I can't for my life figure out where to put the <<xsl:sort> element (alternatively I am using the incorrect select attribute). I must have tried ten different alternatives without success. <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="iso-8859-1"/> <xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:template> <xsl:template match="host|switch"><xsl:variable name="realHostID" select="@name"/><xsl:variable name="deviceType" select="name()"/><xsl:copy><xsl:apply-templates select="@*"/><xsl:choose><xsl:when test="$deviceType = 'host' and string-length($realHostID) > 0"><xsl:apply-templates select="//host/port[starts-with(@name, $realHostID)]"/> </xsl:when> <xsl:otherwise> <xsl:choose> <xsl:when test="$deviceType = 'switch' and string-length($realHostID) > 0"> <xsl:apply-templates select="//switch/port[starts-with(@name, $realHostID)]"/> </xsl:when> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:copy></xsl:template> </xsl:stylesheet> Your help would be deeply appreciated.

Link to comment
Share on other sites

Assuming the input is

<root>  <hosts>    <host>	  <port name="Switch A.Port3" speed="100"/>	  <port name="Switch A.Port1" speed="100"/>	  <port name="Switch A.Port4" speed="100"/>	  <port name="Switch A.Port2" speed="100"/>    </host>  </hosts>  <switches>    <switch modelID="1" name="Switch A">	  <port name="Switch A.Port3" speed="100"/>	  <port name="Switch A.Port1" speed="100"/>	  <port name="Switch A.Port4" speed="100"/>	  <port name="Switch A.Port2" speed="100"/>    </switch>  </switches></root>

then the stylesheet

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"> <xsl:strip-space elements="*"/><xsl:output indent="yes"/><xsl:template match="@* | node()">  <xsl:copy>    <xsl:apply-templates select="@* | node()"/>  </xsl:copy></xsl:template><xsl:template match="hosts/host/port"/><xsl:template match="switches/switch">  <xsl:copy>    <xsl:apply-templates select="@*"/>    <xsl:apply-templates select="port">	  <xsl:sort select="@name" data-type="text"/>    </xsl:apply-templates>  </xsl:copy></xsl:template></xsl:stylesheet>

transforms the input into the result

<?xml version="1.0" encoding="utf-8"?><root>   <hosts>	  <host/>   </hosts>   <switches>	  <switch modelID="1" name="Switch A">		 <port name="Switch A.Port1" speed="100"/>		 <port name="Switch A.Port2" speed="100"/>		 <port name="Switch A.Port3" speed="100"/>		 <port name="Switch A.Port4" speed="100"/>	  </switch>   </switches></root>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...