You don't say clearly what you are doing, so it's hard to answer simply.
If what you mean is that
- you have added a CDATA marked section in an XML document you are feeding to an XSLT stylesheet;
- the portion of the stylesheet's output which corresponds to the CDATA section in the input has references to the entities
lt
and gt
where the input has angle brackets (so <p class="greeting">Hello, world</p>
becomes <p class="greeting">Hello, world!</p>
, and this is what you desire; and
- you would like " not to appear literally in the output either, but be replaced by a reference to the entity
quot
then one way to achieve your aim is to write a template to handle text nodes, which tests for the presence of ", splits the text node into left-part and right-part on the first ", writes out the left part, writes out an ampersand, writes out quot;
, and then calls itself recursively with the right part of the string.
The following stylesheet illustrates the pattern:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="doc">
<xsl:element name="doc">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="text()" name="escape-quot">
<xsl:param name="s" select="."/>
<xsl:choose>
<xsl:when test="contains($s,'"')">
<xsl:variable name="sL"
select="substring-before($s,'"')"/>
<xsl:variable name="sR"
select="substring-after($s,'"')"/>
<xsl:value-of select="$sL"/>
<xsl:text>&quot;</xsl:text>
<xsl:call-template name="escape-quot">
<xsl:with-param name="s" select="$sR"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
We can apply it to the following input to see the result:
<doc>Hi. This is a test.
<![CDATA[<p class="greeting">Hello,
world!</p>]]>
</doc>
The result I get is, I conjecture, what you are looking for.
<?xml version="1.0"?>
<doc><p>Hi. This is a test.</p>
<p><p class=&quot;greeting&quot;>Hello,
world!</p></p>
</doc>
If that wasn't what you wanted, you might try explaining your question in more detail. It's always a good idea in cases like this to provide (a) the key bits of your current code, (b) sample input, (c) a sample of the output you're currently getting, with a description of what's wrong with it, and (d) a sample of what you would like the output to look like. (Keep both the samples and the code short -- you want to provide the smallest possible complete working example so readers can recreate your problem.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…