Considering this XML code:
<root>
blah <foo>blah</foo> blah <bar>blah</bar> blah
</root>
And his associated stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="foo">
<strong><xsl:apply-templates/></strong>
</xsl:template>
</xsl:stylesheet>
After transofrmation with the XSLTProcessor class (PHP), here is the output:
blah <strong>blah</strong> blah blah blah
But I rather want this output (unknown elements in the stylesheet are escaped):
blah <strong>blah</strong> blah <bar>blah</bar> blah
My pseudocode proposal:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="foo">
<strong><xsl:apply-templates/></strong>
</xsl:template>
<xsl:template match="all elements other than foo (with their attributs :p)">
<xsl:copy-of select="node()" escape="yes"/>
</xsl:template>
</开发者_运维百科xsl:stylesheet>
I'm desperate, so if you have any solution for keeping and escaping those useless elements, I'll be very happy!
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="foo">
<strong><xsl:value-of select="."/></strong>
</xsl:template>
<xsl:template match="*">
<xsl:text><</xsl:text>
<xsl:value-of select="local-name(.)"/>
<xsl:apply-templates select="@*"/>
<xsl:text>></xsl:text>
<xsl:apply-templates select="node()"/>
<xsl:text></</xsl:text>
<xsl:value-of select="local-name(.)"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="@*">
<xsl:text> </xsl:text>
<xsl:value-of select="name()" />
<xsl:text>="</xsl:text>
<xsl:value-of select="." />
<xsl:text>"</xsl:text>
</xsl:template>
<xsl:template match="comment()">
<xsl:text><!--</xsl:text>
<xsl:value-of select="."/>
<xsl:text>--></xsl:text>
</xsl:template>
<xsl:template match="processing-instruction()">
<xsl:text><? </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text>?></xsl:text>
</xsl:template>
</xsl:stylesheet>
Wrote this off the top of my head, so please don't quote me on that :D
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="foo">
<strong><xsl:value-of select="."/></strong>
</xsl:template>
<xsl:template match="node()">
<<xsl:value-of select="local-name(.)"/>><xsl:value-of select="."/></<xsl:value-of select="local-name(.)"/>>
</xsl:template>
</xsl:stylesheet>
精彩评论