ramu_src2k
asked on
Re-arrange xml elements (with Namespaces) using XSLT
I need to rearrange the below XML along with the namespaces. In my earlier post (https://www.experts-exchange.com/questions/25089800/Re-arrange-xml-elements-based-on-attributes-using-XSLT.html) i was able to rearrange the xml without namespaces . Can any one help in this regard.
Thanks in Advance
Input XML:
<?xml version="1.0" encoding="UTF-16"?>
<sport:codes xmlns:sport="icc:ft">
<sport:Entity sport:name="Cricket" >
<somedata11 xmlns="abc">Raghu</somedat a11>
<somedata22 xmlns="abc">Hemanth</somed ata22>
</sport:Entity>
<sport:Entity sport:name="Golf">
<somedata11
xmlns:test="xxx"><test:dat a21>Shilpa </test:dat a21></some data11>
<somedata22
xmlns:test="xxx"><test:dat a22>Sudha< /test:data 22></somed ata22>
</sport:Entity>
<sport:Entity sport:name="Football">
<somedata11 xmlns:t1="t1ns" xmlns:t2="t2ns">Ramesh</so medata11>
<somedata22 xmlns:t1="t1ns" xmlns:t2="t2ns">Sivakumar< /somedata2 2>
</sport:Entity>
<sport:Entity sport:name="Tennis">
<somedata11>John</somedata 11>
<somedata22>Peter</somedat a22>
</sport:Entity>
</sport:codes>
XSLT used:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sport="icc:ft" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" encoding="UTF-16"/>
<xsl:template match="sport:codes">
<sport:codes xmlns:sport="icc:ft">
<xsl:for-each select="sport:Entity">
<xsl:sort
select="(number(@sport:ent ityname='F ootball') * 1)
+ (number(@sport:entityname= 'Tennis') * 2)
+ (number(@sport:entityname= 'Golf') * 3)
+ (number(@sport:entityname= 'Cricket') * 4)"
order="ascending" data-type="number" />
<xsl:copy-of select="."/>
</xsl:for-each>
</sport:codes>
</xsl:template>
</xsl:stylesheet>
Expected Output XML:
<?xml version="1.0" encoding="UTF-16"?>
<sport:codes xmlns:sport="icc:ft">
<sport:Entity sport:name="Football">
<somedata11 xmlns:t1="t1ns" xmlns:t2="t2ns">Ramesh</so medata11>
<somedata22 xmlns:t1="t1ns" xmlns:t2="t2ns">Sivakumar< /somedata2 2>
</sport:Entity>
<sport:Entity sport:name="Tennis">
<somedata11>John</somedata 11>
<somedata22>Peter</somedat a22>
</sport:Entity>
<sport:Entity sport:name="Golf">
<somedata11
xmlns:test="xxx"><test:dat a21>Shilpa </test:dat a21></some data11>
<somedata22
xmlns:test="xxx"><test:dat a22>Sudha< /test:data 22></somed ata22>
</sport:Entity>
<sport:Entity sport:name="Cricket" >
<somedata11 xmlns="abc">Raghu</somedat a11>
<somedata22 xmlns="abc">Hemanth</somed ata22>
</sport:Entity>
</sport:codes>
Thanks in Advance
Input XML:
<?xml version="1.0" encoding="UTF-16"?>
<sport:codes xmlns:sport="icc:ft">
<sport:Entity sport:name="Cricket" >
<somedata11 xmlns="abc">Raghu</somedat
<somedata22 xmlns="abc">Hemanth</somed
</sport:Entity>
<sport:Entity sport:name="Golf">
<somedata11
xmlns:test="xxx"><test:dat
<somedata22
xmlns:test="xxx"><test:dat
</sport:Entity>
<sport:Entity sport:name="Football">
<somedata11 xmlns:t1="t1ns" xmlns:t2="t2ns">Ramesh</so
<somedata22 xmlns:t1="t1ns" xmlns:t2="t2ns">Sivakumar<
</sport:Entity>
<sport:Entity sport:name="Tennis">
<somedata11>John</somedata
<somedata22>Peter</somedat
</sport:Entity>
</sport:codes>
XSLT used:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sport="icc:ft" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" encoding="UTF-16"/>
<xsl:template match="sport:codes">
<sport:codes xmlns:sport="icc:ft">
<xsl:for-each select="sport:Entity">
<xsl:sort
select="(number(@sport:ent
+ (number(@sport:entityname=
+ (number(@sport:entityname=
+ (number(@sport:entityname=
order="ascending" data-type="number" />
<xsl:copy-of select="."/>
</xsl:for-each>
</sport:codes>
</xsl:template>
</xsl:stylesheet>
Expected Output XML:
<?xml version="1.0" encoding="UTF-16"?>
<sport:codes xmlns:sport="icc:ft">
<sport:Entity sport:name="Football">
<somedata11 xmlns:t1="t1ns" xmlns:t2="t2ns">Ramesh</so
<somedata22 xmlns:t1="t1ns" xmlns:t2="t2ns">Sivakumar<
</sport:Entity>
<sport:Entity sport:name="Tennis">
<somedata11>John</somedata
<somedata22>Peter</somedat
</sport:Entity>
<sport:Entity sport:name="Golf">
<somedata11
xmlns:test="xxx"><test:dat
<somedata22
xmlns:test="xxx"><test:dat
</sport:Entity>
<sport:Entity sport:name="Cricket" >
<somedata11 xmlns="abc">Raghu</somedat
<somedata22 xmlns="abc">Hemanth</somed
</sport:Entity>
</sport:codes>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
note that my approach with the translate only works if the first letter of each word to sort is distinct
(otherwise you need to expand by also translating teh second letter!)
I prefer my earliest suggestion
There is a way in between by putting all names in a parameter
something like this
<xsl:param name="sortstring">#Footbal l=1#Tennis =2#Golf=3# Cricket=4# </xsl:para m>
This way I emulate the lookup table in one string, instead of a full table
you can use substring-before and substring-after to actually get the sort done
All of my alternatives give more flexible and more readable XSLT, over your sum,
but that could be a matter of taste
(otherwise you need to expand by also translating teh second letter!)
I prefer my earliest suggestion
There is a way in between by putting all names in a parameter
something like this
<xsl:param name="sortstring">#Footbal
This way I emulate the lookup table in one string, instead of a full table
you can use substring-before and substring-after to actually get the sort done
All of my alternatives give more flexible and more readable XSLT, over your sum,
but that could be a matter of taste
weird decission this is
basically all my comments are good for a solution
you can safely accept ID:26436364
basically all my comments are good for a solution
you can safely accept ID:26436364
weird decission this is
basically all my comments are good for a solution
you can safely accept ID:26436364
basically all my comments are good for a solution
you can safely accept ID:26436364
Open in new window