1

このように入力があります

<xml>
<p>"It may be recalled that the foggy family law suit in Jarndyce v Jarndyce dragged on before the Lord Chancellor for generations until nothing was left for the parties to take. </p>
</xml> 

これを次のように変換する必要があります[つまり、json形式]:

"content": "<p>&#x0022;It may be recalled that the foggy family law suit in Jarndyce v Jarndyce dragged on before the Lord Chancellor for generations until nothing was left for the parties to take&#x0022;. </p>"

つまり、ここでは、段落内でのみ引用符が必要です。ここ以外は変更しないでください。何か案は?

4

2 に答える 2

1

これは XSLT 1.0 ソリューションです - 再帰的なテンプレートを使用して文字列の置換を行います:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template name="replace">
    <xsl:param name="str"/>
    <xsl:param name="from"/>
    <xsl:param name="to"/>
    <xsl:choose>
      <xsl:when test="contains($str,$from)">
        <xsl:value-of select="concat(substring-before($str,$from),$to)"/>
        <xsl:call-template name="replace">
          <xsl:with-param name="str" select="substring-after($str,$from)"/>
          <xsl:with-param name="from" select="$from"/>
          <xsl:with-param name="to" select="$to"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$str"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="p">
    "content" : "&lt;p&gt;
    <xsl:call-template name="replace">
      <xsl:with-param name="str" select="."/>
      <xsl:with-param name="from" select="'&quot;'"/>
      <xsl:with-param name="to" select="'&amp;#x0022;'"/>
    </xsl:call-template>
    &lt;/p&gt;"
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>

</xsl:stylesheet>
于 2013-08-27T15:08:11.297 に答える
0

別の xsl 1.0 ソリューション

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes" method="text"/>
  <xsl:template match="/xml/p">
      <xsl:text>"content":"&lt;p&gt;</xsl:text>
      <xsl:call-template name="replace">
        <xsl:with-param name="substring" select="text()"/>
      </xsl:call-template>
      <xsl:text>&lt;/p&gt;"</xsl:text>
  </xsl:template>
  <xsl:template name="replace">
    <xsl:param name="substring"/> 
      <xsl:choose>
        <xsl:when test="contains($substring,'&quot;')">
          <xsl:value-of select="substring-before($substring,'&quot;')"/>
          <xsl:text>&amp;#x0022;</xsl:text>
          <xsl:call-template name="replace">
            <xsl:with-param name="substring" select="substring-after($substring,'&quot;')"/>
          </xsl:call-template>  
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$substring"/>
        </xsl:otherwise>
      </xsl:choose> 
  </xsl:template> 
</xsl:stylesheet>

ここでテストできます

于 2013-08-27T15:18:36.240 に答える