我有一个Sharepoint列表,我想通过XSL数据视图转换为JSON。用<br />替换新的行字符XSL
我有一个XSL递归替换函数,我用它来替换所有特殊字符(转义反斜杠,双引号到&“等),这给了我很好的干净的JSON在用户浏览器中正确解析。
我需要逃避/替换的最后一件事是新行字符。新行会在某些浏览器中解析JSON时导致错误。
下面是一些XSL在此进行测试,如果标题的内容有一个新行字符,如果是这样,我们输出的一段:
<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>
似乎是一个重复:http://stackoverflow.com/questions/3309746 – mzjn 2011-05-15 16:32:27
无法用提供的输入重现。对于示例XML和示例XSLT,它仅匹配“Empire Burlesque”标题。我没有在输出中获得所有三个标题。您确定示例XML与您的实际数据相匹配吗? – 2011-05-15 17:05:39
好问题,+1。查看我的答案,获取完整,简短的解决方案和详细解释。 – 2011-05-15 17:46:14