XSL替换换行符

| 我有一个共享点列表,我想通过XSL数据视图将其转换为JSON。 我有一个XSL递归替换函数,可用来替换所有特殊字符(转义反斜杠,双引号等),这给了我很好的干净的JSON,可以在用户浏览器中正确解析。 我需要转义/替换的最后一件事是换行符。新行在某些浏览器中导致解析JSON时出错。 这是一些xsl,用于测试title的内容是否具有换行符char(如果确实输出了一个段落):
<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">

<xsl:template match=\"/\">

    <xsl:for-each select=\"catalog/cd\">

        <xsl:if test=\'contains(title,\"&#xA;\")\'>   
                <p>Found <xsl:value-of select=\"title\" /></p>
        </xsl:if>

    </xsl:for-each>

</xsl:template>
</xsl:stylesheet>
这是一些示例xml:
<catalog>
    <cd>
        <title>Empire 
Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>
\“ Empire Burlesque \”应该是唯一可以通过测试的项目,但是所有三个标题都可以通过if语句并输出。 编辑 修改下面的解决方案,如果我想在单个节点上进行搜索和替换,我认为这应该可行。直到明天我才能在Sharepoint中对其进行测试。
<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
<xsl:output omit-xml-declaration=\"yes\" indent=\"yes\"/>
<xsl:strip-space elements=\"*\"/>

<xsl:template match=\"/\">
  <xsl:for-each select=\"catalog/cd\">

  <xsl:variable name=\"title_clean\">
    <xsl:call-template name=\"repNL\">
      <xsl:with-param name=\"pText\" select=\"title\"/>
    </xsl:call-template>
  </xsl:variable>

  <p><xsl:value-of select=\'$title_clean\' /></p>

  </xsl:for-each>
</xsl:template>


<xsl:template name=\"repNL\">
  <xsl:param name=\"pText\" select=\".\"/>

  <xsl:copy-of select=\"substring-before(concat($pText,\'&#xA;\'),\'&#xA;\')\"/>

  <xsl:if test=\"contains($pText, \'&#xA;\')\">
  <br />
  <xsl:call-template name=\"repNL\">
  <xsl:with-param name=\"pText\" select=
  \"substring-after($pText, \'&#xA;\')\"/>
  </xsl:call-template>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>
    
已邀请:
  “帝国滑稽演员”应该是唯一的   通过测试的项目,但全部三个   标题通过if语句,并且是   输出。 无法重现所谓的问题-使用9种不同的XSLT处理器(包括来自Microsoft的所有处理器)进行了测试。 无论如何: 此转换用node3ѭ元素替换文本节点中的所有NL字符:
<xsl:stylesheet version=\"1.0\"
 xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
 <xsl:output omit-xml-declaration=\"yes\" indent=\"yes\"/>
 <xsl:strip-space elements=\"*\"/>

 <xsl:template match=\"node()|@*\">
  <xsl:copy>
   <xsl:apply-templates select=\"node()|@*\"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match=\"text()\" name=\"repNL\">
  <xsl:param name=\"pText\" select=\".\"/>

  <xsl:copy-of select=
  \"substring-before(concat($pText,\'&#xA;\'),\'&#xA;\')\"/>

  <xsl:if test=\"contains($pText, \'&#xA;\')\">
   <br />
   <xsl:call-template name=\"repNL\">
    <xsl:with-param name=\"pText\" select=
    \"substring-after($pText, \'&#xA;\')\"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
当应用于以下XML文档时(提供的文档中加上了ѭ5使其更加有趣):
<catalog>
    <cd>
        <title>Line1
        Line2
        Line3
        </title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Empire
        Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>
所需的正确结果产生了:
<catalog>
   <cd>
      <title>Line1<br/>     Line2<br/>      Line3<br/>      
      </title>
      <artist>Bob Dylan</artist>
      <country>USA</country>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
   </cd>
   <cd>
      <title>Empire<br/>        Burlesque</title>
      <artist>Bob Dylan</artist>
      <country>USA</country>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
   </cd>
   <cd>
      <title>Hide your heart</title>
      <artist>Bonnie Tyler</artist>
      <country>UK</country>
      <company>CBS Records</company>
      <price>9.90</price>
      <year>1988</year>
   </cd>
   <cd>
      <title>Greatest Hits</title>
      <artist>Dolly Parton</artist>
      <country>USA</country>
      <company>RCA</company>
      <price>9.90</price>
      <year>1982</year>
   </cd>
</catalog>
说明: 身份规则/模板“按原样”复制每个节点。 与任何文本节点(也称为\“ repNL \”)匹配的覆盖模板执行以下处理: 使用前哨(在字符串后附加一个NL字符),将第一个NL字符(或完整的字符串,如果不包含NL字符)之前的子字符串复制到输出。 如果确实包含一个NL字符,则将生成一个
br
元素,并且模板将在该NL字符之后为其余字符串递归调用自身。     
如果您使用的是XslCompiledTransform(C#),我相信如果您使用下面的API来转换XML,就会遇到问题:
XslCompiledTransform.Transform(XmlReader input, XmlWriter results)
但是,下面的API效果很好:
XslCompiledTransform.Transform(string, string);
这是有线的,但我不知道为什么...     

要回复问题请先登录注册