want to replace " with " from a xml

旧城冷巷雨未停 提交于 2020-01-07 08:25:35

问题


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;


回答1:


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.)



来源:https://stackoverflow.com/questions/14217025/want-to-replace-with-quot-from-a-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!