Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

xslt 1.0 - want to replace " with " from a xml

Hi i have applied CDATA to a sectio of nodes in a xml all the < and > are replaced by &lt; and &gt;. I want to replace " with &quot; in addition.

what changes do i need to make to the CDATA part to replace " with &quot;

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 &lt;p class="greeting"&gt;Hello, world!&lt;/p&gt;, 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,'&quot;')">
        <xsl:variable name="sL" 
          select="substring-before($s,'&quot;')"/>
        <xsl:variable name="sR" 
          select="substring-after($s,'&quot;')"/>
        <xsl:value-of select="$sL"/>
        <xsl:text>&amp;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>&lt;p class=&amp;quot;greeting&amp;quot;&gt;Hello, 
world!&lt;/p&gt;</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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...