xslt - Categorize XML based on element attribute type -
with xml input unable add elements specific sections.
<country> <info enum="ctry" name="united sates of america" total-states="50" /> <info enum="st" name="new york" population="8,244,910"/> <info enum="st" name="chicago" population="2,707,120"/> <info enum="ctry" name="germany" total-states="16"/> <info enum="st" name="berlin" population="3,469,910"/> <info enum="st" name="brandenburg" population="2,500,000"/> </country>
here xsl,
<xsl:template match="/"> <country> <xsl:for-each select="country/info"> <xsl:if test="@enum='ctry'"> <countryinfo> <name>country name: <xsl:value-of select="@name"/></name> <districts><xsl:value-of select="@total-states"></xsl:value-of></districts> <xsl:for-each select="/country/info"> <xsl:if test="@enum='st'"> <state> <statename>state name: <xsl:value-of select="@name"/></statename> <statepop>state population: <xsl:value-of select="@population"/></statepop> </state> </xsl:if> </xsl:for-each> </countryinfo> </xsl:if> </xsl:for-each> </country> </xsl:template>
the problem states showing both countries.
here xml output trying generate,
<country> <countryinfo> <name>country name: united sates of america</name> <districts>50</districts> <state> <statename>state name: new york</statename> <statepop>state population: 8,244,910</statepop> </state> <state> <statename>state name: chicago</statename> <statepop>state population: 2,707,120</statepop> </state> </countryinfo> <countryinfo> <name>country name: germany</name> <districts>16</districts> <state> <statename>state name: berlin</statename> <statepop>state population: 3,469,910</statepop> </state> <state> <statename>state name: brandenburg</statename> <statepop>state population: 2,500,000</statepop> </state> </countryinfo> </country>
is possible accomplish xslt?
your source dreadful abuse of xml! should complain bitterly whoever designed , provided such rubbish.
with single template can't more remove or expand on elements in source.
i believe transform need. works copying country
root element , processing contents. second template matches of info
elements have enum
attribute of ctry
, form basis countryinfo
output elements.
the state information has done recursively, using call-template
insert information following info
element if has enum
attribute of st
.
because of structure of source data transform fragile, , break if there unexpected elements. please beware.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="/country"> <xsl:copy> <xsl:apply-templates select="*"/> </xsl:copy> </xsl:template> <xsl:template match="info[@enum='ctry']"> <countryinfo> <name> <xsl:text>country name: </xsl:text> <xsl:value-of select="@name"/> </name> <districts> <xsl:value-of select="@total-states"/> </districts> <xsl:call-template name="state"/> </countryinfo> </xsl:template> <xsl:template name="state"> <xsl:param name="i" select="1"/> <xsl:if test="following-sibling::info[$i][@enum='st']"> <state> <statename> <xsl:text>state name: </xsl:text> <xsl:value-of select="following-sibling::info[$i]/@name"/> </statename> <statepop> <xsl:text>state population: </xsl:text> <xsl:value-of select="following-sibling::info[$i]/@population"/> </statepop> </state> <xsl:call-template name="state"> <xsl:with-param name="i" select="$i+1"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>
output
<?xml version="1.0" encoding="utf-8"?> <country> <countryinfo> <name>country name: united states of america</name> <districts>50</districts> <state> <statename>state name: new york</statename> <statepop>state population: 8,244,910</statepop> </state> <state> <statename>state name: chicago</statename> <statepop>state population: 2,707,120</statepop> </state> </countryinfo> <countryinfo> <name>country name: germany</name> <districts>16</districts> <state> <statename>state name: berlin</statename> <statepop>state population: 3,469,910</statepop> </state> <state> <statename>state name: brandenburg</statename> <statepop>state population: 2,500,000</statepop> </state> </countryinfo> </country>
Comments
Post a Comment