开发者

How to keep AND escape unmatched elements?

开发者 https://www.devze.com 2023-03-25 16:14 出处:网络
Considering this XML code: <root> blah <foo>blah</foo> blah <bar>blah</bar> blah

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 &lt;bar&gt;blah&lt;/bar&gt; 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>&lt;</xsl:text>
            <xsl:value-of select="local-name(.)"/>
            <xsl:apply-templates select="@*"/>
        <xsl:text>&gt;</xsl:text>

        <xsl:apply-templates select="node()"/>

        <xsl:text>&lt;/</xsl:text>
            <xsl:value-of select="local-name(.)"/>
        <xsl:text>&gt;</xsl:text>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:text>&#160;</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>&lt;!--</xsl:text>
         <xsl:value-of select="."/>
          <xsl:text>--&gt;</xsl:text>
    </xsl:template>

    <xsl:template match="processing-instruction()">
         <xsl:text>&lt;? </xsl:text>
         <xsl:value-of select="name()"/>
         <xsl:text>&#160;</xsl:text>
         <xsl:value-of select="."/>
          <xsl:text>?&gt;</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()">
        &lt;<xsl:value-of select="local-name(.)"/>&gt;<xsl:value-of select="."/>&lt;/<xsl:value-of select="local-name(.)"/>&gt;
    </xsl:template>

</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消