【发布时间】:2011-05-15 16:12:55
【问题描述】:
我有一个 Sharepoint 列表,我想通过 XSL 数据视图将其转换为 JSON。
我有一个 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>
“帝国滑稽剧”应该是唯一通过测试的项目,但是三个标题都通过了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>
【问题讨论】:
-
无法使用提供的输入进行复制。对于示例 XML 和您的示例 XSLT,它仅匹配“Empire Burlesque”标题。我没有在输出中得到所有三个标题。您确定示例 XML 与您的实际数据匹配吗?
-
好问题,+1。请参阅我的答案以获得完整、简短且简单的解决方案和详细说明。
-
结果可以复制到w3schools.com/xsl/…
标签: sharepoint xslt dataview