Sorting XML with XSLT - entire XML-schema is not known -
Sorting XML with XSLT - entire XML-schema is not known -
i wondering whether xslt makes possible sort xml file if don't know entire xml-schema.
for illustration sort next xml file. sort /catalog/cd elements /catalog/cd/title
<catalog attrib1="value1"> <dvd2> <title>the godfather2</title> </dvd2> <cd> <title>hide heart</title> <artist>bonnie tyler</artist> <country>uk</country> <company>cbs records</company> <price>9.90</price> <year>1988</year> </cd> <cd attrib4="value4"> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price> <catalog> <cd><title>e</title></cd> <cd><title>i</title></cd> <cd><title>d</title></cd> </catalog> </price> <year>1985</year> </cd> <cd attrib2="value2"> <title attrib3="value3">greatest hits</title> <artist>dolly parton</artist> <country>usa</country> <company>rca</company> <price>9.90</price> <year>1982</year> </cd> <dvd> <title>the godfather1</title> </dvd> </catalog>
the output should be:
<catalog attrib1="value1"> <cd attrib4="value4"> <title>empire burlesque</title> <artist>bob dylan</artist> <country>usa</country> <company>columbia</company> <price> <catalog> <cd><title>e</title></cd> <cd><title>i</title></cd> <cd><title>d</title></cd> </catalog> </price> <year>1985</year> </cd> <cd attrib2="value2"> <title attrib3="value3">greatest hits</title> <artist>dolly parton</artist> <country>usa</country> <company>rca</company> <price>9.90</price> <year>1982</year> </cd> <cd> <title>hide heart</title> <artist>bonnie tyler</artist> <country>uk</country> <company>cbs records</company> <price>9.90</price> <year>1988</year> </cd> <dvd2> <title>the godfather2</title> </dvd2> <dvd> <title>the godfather1</title> </dvd> </catalog>
the next 1 of many tries did:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <!--<catalog>--> <xsl:for-each select="catalog/cd"> <xsl:sort select="title" /> <xsl:copy-of select="."/> </xsl:for-each> <!--</catalog>--> </xsl:template> </xsl:stylesheet>
the problem that, xslt, xml parts outside cd list not displayed. uncomment 2 commented-out parts of code, that's want avoid. in case if attributes added catalog element, not copied output xml. don't want re-build xml file: want sort knowing exact info part of xml-schema.
this functionality easy implement illustration using .net (with xmldocument , xmlnode objects), or python's lxmx library, possible xslt?
thanks!
note: not easy find sample input xml avoid misunderstanding question in cases. seek detail problem much can:
only cd elements right under catalog should sorted (for illustration cd elements under bob dylan section should left untouched) it same whether elements other cd (for illustration dvd , dvd2) in origin or end of list no elements, attributes, values, comments, nil should missing output xml non-cd elements (for illustration dvd , dvd2) should not sorted title subelement
keeping on line of modifying identity transformation (which might not safe), think next should equivalent @tim's answer.
note i'm not promoting technique @ all, unless understand what's general behavior of identity transformation.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()[not(self::cd[parent::catalog])]"/> <xsl:apply-templates select="cd[parent::catalog]"> <xsl:sort select="title"/> </xsl:apply-templates> </xsl:copy> </xsl:template> </xsl:stylesheet>
or, if care other elements dvd
, dvd2
, can do:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="cd[parent::catalog]"> <xsl:sort select="title"/> </xsl:apply-templates> <xsl:apply-templates select="node() [not(self::cd[parent::catalog])]"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
xml xslt sorting
Comments
Post a Comment