An XSLT-newbie problem: I need to substitute a text value in XML-file. All other nodes must be left unchanged. Here’s my input file (in.xml):
<?xml version='1.0' encoding='UTF-8'?> <root> <level1 attr1='val1'> <level2>in</level2> </level1> </root>
Here’s my XSLT-transformation (subst.xsl):
<?xml version='1.0' encoding='UTF-8'?> <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match='/'> <xsl:apply-templates /> </xsl:template> <xsl:template match='//node()'> <xsl:copy> <xsl:apply-templates /> </xsl:copy> </xsl:template> <xsl:template match='/root/level1/level2/text()'>out</xsl:template> </xsl:stylesheet>
I run it with the following Ant-script (build.xml):
<?xml version='1.0' encoding='UTF-8'?> <project name='test' default='test' basedir='.'> <target name='test'> <xslt style='subst.xsl' in='in.xml' out='out.xml' /> </target> </project>
And here’s what I get (out.xml):
<?xml version='1.0' encoding='UTF-8'?><root> <level1> <level2>out</level2> </level1> </root>
Attribute ‘attr1’ of ‘level1’ is missing.
I would be very grateful if anyone
- tell me what’s wrong with subst.xsl
OR
- give me an idea how to force xslt-processor just copy non-matched nodes to output file and to do it by hand (which is in my case error-prone).
Your identity transform is missing attributes (obviously). Use this instead:
Then, just add your last template.