XSLT 1.0 Extract values from nested XML -
i'm trying specific xml existing one. please?
source xml:
<?xml version="1.0" encoding="utf-8"?> <row> <column name="testname">test 1</column> <column name="tests"> <xmldoc xmlns="customers servers"> <tests> <test> <cufirst>hardware</cufirst> <culast>server 1</culast> </test> <test> <cufirst>hardware</cufirst> <culast>server 2</culast> </test> </tests> </xmldoc> </column> <column name="company">some company</column> <column name="company address" /> <column name="company url" /> <column name="company alias">company alias</column> </row>
current xslt (not working expected); did try different things, nothing; continue trying, if have idea how help, please do.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output indent="no" /> <xsl:template match="/"> <tests> <xsl:for-each select="//column[@name='tests']"> <ci> <xsl:value-of select="//column[@name='tests']/xmldoc/tests/test//culast" disable-output-escaping="no" /> </ci> </xsl:for-each> <test> <xsl:value-of select="//column[@name='testname']" disable-output-escaping="no" /> </test> </tests> </xsl:template> </xsl:stylesheet>
desired output xml
<tests> <test> <culast>test 1</culast> </test> <test> <culast>server 1</culast> </test> <test> <culast>server 2</culast> </test> </tests>
first thing: xmldoc
element , descendants live in namespace of own; must declare namespace in stylesheet, assign prefix , use prefix when addressing these nodes.
also, in xslt pays explicit: avoid // designation if know exact path.
try following stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:cs="customers servers" exclude-result-prefixes="cs"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <tests> <test><xsl:value-of select="row/column[@name='testname']"/></test> <xsl:for-each select="row/column[@name='tests']/cs:xmldoc/cs:tests/cs:test"> <ci> <xsl:value-of select="cs:culast" /> </ci> </xsl:for-each> </tests> </xsl:template> </xsl:stylesheet>
when applied input, result be:
<?xml version="1.0" encoding="utf-8"?> <tests> <test>test 1</test> <ci>server 1</ci> <ci>server 2</ci> </tests>
note "customers servers"
not valid uri , therefore not choice namespace. not sure if have control on that, though.
Comments
Post a Comment