Reputation: 351
I'm using the following xsl transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
<xsl:template match="//something">
<mytag>
<xsl:copy-of select="node()"/>
</mytag>
</xsl:template>
</xsl:stylesheet>
It produces the wished content:
<mytag>
<something ...
</mytag>
<mytag>
<something ...
</mytag>
...
But additionally there are all contents of the xml file around the wished results. E.g. for the source file:
<something>Hello</something>
<anotherthing>Bye</anotherthing>
I get the result (or something similar):
<mytag>
<something>Hello</something>
</mytag>
Hello
Bye
I tested the transformation with java.xml.transform.Transformer and Oxygen.
Thanks for any help!
Upvotes: 1
Views: 74
Reputation: 122414
The problem is that there's a set of "default" template rules that apply to any nodes you haven't got an explicit template for. If you're only interested in the something
elements then the simplest approach is to add a root template that ensures you're only applying templates to those, and not to the other nodes that you don't care about:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="//something" />
</root>
</xsl:template>
<xsl:template match="something">
<mytag>
<xsl:copy-of select="node()"/>
</mytag>
</xsl:template>
</xsl:stylesheet>
(Note I'm also adding a single root-level element around your results, as otherwise the result isn't well-formed when there's more than one something
in the input).
Upvotes: 4