nested xml tag to xslt -
can me on this? have xml this
<x:person> <x:name>angel</x:name> <x:education-info> <school>school abc</school> <address>address 123</address> <city>city 1</city> <year>2001</year> <remarks>12334</remarks> <school>school abc2</school> <address>address 456</address> <city>city 2</city> <year>2005</year> <remarks>test1233</remarks> </x:education-info> <x:age>22</x:age> </x:person>
how can school , address tag inside education-info tag ? , format xslt this
school abc - address 123 school abc2 - address 456
xslt 1.0
if first make xml valid xml, following, has effect elements in namespace urn:yourns
, in no namespace:
<x:person xmlns:x="urn:yourns"> <x:name>angel</x:name> <x:education-info> <school>school abc</school> <address>address 123</address> <city>city 1</city> <year>2001</year> <remarks>12334</remarks> <school>school abc2</school> <address>address 456</address> <city>city 2</city> <year>2005</year> <remarks>test1233</remarks> </x:education-info> <x:age>22</x:age> </x:person>
now, if want data input, need make sure match proper namespace in xslt. not have match prefix, long matches namespace.
the following code shows how "xslt way" using templates. may more code first answer, more resilient change:
<xsl:stylesheet version="1.0" xmlns:your="urn:yourns" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <!-- select 'text', output suggests want text --> <xsl:output method="text" /> <!-- ignore aren't interested in (this 1 not req. data, if input larger/different) --> <xsl:template match="node()"> <xsl:apply-templates /> </xsl:template> <xsl:template match="text()" /> <!-- match parent x:education-info select interested in (this 1 can left out well, more cleanly shows want do)--> <xsl:template match="your:education-info"> <xsl:apply-templates select="school | address" /> </xsl:template> <!-- match school in no-namespace --> <xsl:template match="school"> <xsl:value-of select="." /> <xsl:text> - </xsl:text> </xsl:template> <!-- match address in no-namespace --> <xsl:template match="address"> <xsl:value-of select="." /> <xsl:text>
</xsl:text> </xsl:template> </xsl:stylesheet>
the output, when run against input following. check online clicking here.
school abc - address 123 school abc2 - address 456
xslt 3.0
for fun of it, or because example cut out call on for-each-pair
, here's one-liner (if ignore bloat) in xslt 3.0:
<xsl:stylesheet version="3.0" xmlns:your="urn:yourns" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" /> <xsl:mode on-no-match="shallow-skip" /> <xsl:template match="your:education-info"> <xsl:value-of separator="
" select=" for-each-pair(school, address, function($a, $b) {$a || ' - ' || $b })" /> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment