I would like to remove all elements from XML except content of element called <source>. E.g.:
<root>
<a>This will be stripped off</a>
<source>But this not</source>
</root>
After XSLT:
But this not
I have tried this but with no luck (no output):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="source">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
</xsl:stylesheet>
From comments:
In my real XML I have the source
element in different namespace. I need
to google how to create a match
pattern for element in different
namespace. I would like to put each
extracted string also to newline 😉
You’re not far off. The reason you’re not getting any output is because your root match-all template isn’t recursing but just terminating so you need to put an
apply-templatescall inside it. The following stylesheet gives the expected output.Note that I’ve changed the output mode to
textand thesourcetemplate to simply output the textual value of the node, because it looks like you want text and not XML output.