用
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,\"
\")\'>
<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,\'
\'),\'
\')\"/>
<xsl:if test=\"contains($pText, \'
\')\">
<br />
<xsl:call-template name=\"repNL\">
<xsl:with-param name=\"pText\" select=
\"substring-after($pText, \'
\')\"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
没有找到相关结果
已邀请:
2 个回复
抽法
当应用于以下XML文档时(提供的文档中加上了ѭ5使其更加有趣):
所需的正确结果产生了:
说明: 身份规则/模板“按原样”复制每个节点。 与任何文本节点(也称为\“ repNL \”)匹配的覆盖模板执行以下处理: 使用前哨(在字符串后附加一个NL字符),将第一个NL字符(或完整的字符串,如果不包含NL字符)之前的子字符串复制到输出。 如果确实包含一个NL字符,则将生成一个
元素,并且模板将在该NL字符之后为其余字符串递归调用自身。
缔恃钨
但是,下面的API效果很好:
这是有线的,但我不知道为什么...