I have something like this :
<body>
foo bar foo bar foo bar...
<p>foo bar!</p>
<div class="iWantYourContent">
<p>baz</p>
</div>
</body>
I want this output:
<body>
foo bar foo bar foo bar...
<p>foo bar!</p>
<p>baz</p>
</body>
I have managed to get the content of the node using this :
<xsl:template match="/">
<xsl:apply-templates select="//x:div[@class = 'iWantYourContent']"/>
</xsl:template>
<xsl:template match="//x:div[@class = 'iWantYourContent']">
<body>
<xsl:copy-of select="node()"/>
</body>
</xsl:template>
But i'm not able to keep the rest of the document.
Thanks for helping.
The way to do this sort of thing is typically, with an identity template that copies everything:
<xsl:template match="node()|@*" >
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
And then you make a template to match the items you want to skip over:
<xsl:template match="div[@class='iWantYourContent']" >
<xsl:apply-templates select="*" />
</xsl:template>
i.e. skip the copy, because you don't want the div element copies, but DO apply templates
on further elements, because you do want to copy the descendants of div.
( If you wanted to skip the contents entirely, then you would leave the template empty and have no content output at all. )
If you are only interested in the plain text and the <p>
nodes, use this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Suppress the xml header in output -->
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="/">
<body><xsl:apply-templates /></body>
</xsl:template>
<xsl:template match="p">
<p><xsl:copy-of select="text()"/></p>
</xsl:template>
</xsl:stylesheet>
I've used the commandline tool xsltproc
for testing the stylesheet:
xsltproc test.xsl test.html
Output:
<body>
foo bar foo bar foo bar...
<p>foo bar!</p>
<p>baz</p>
</body>