2011-05-15 108 views
2

我有一个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,"&#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> 
+0

似乎是一个重复:http://stackoverflow.com/questions/3309746 – mzjn 2011-05-15 16:32:27

+0

无法用提供的输入重现。对于示例XML和示例XSLT,它仅匹配“Empire Burlesque”标题。我没有在输出中获得所有三个标题。您确定示例XML与您的实际数据相匹配吗? – 2011-05-15 17:05:39

+0

好问题,+1。查看我的答案,获取完整,简短的解决方案和详细解释。 – 2011-05-15 17:46:14

回答

2

“帝国滑稽表演”应该是唯一的 项目通过测试,但所有这三个 冠军通过if语句,并输出 。

无法重现涉嫌问题 - 9个不同的XSLT处理器,包括所有从微软测试。

反正:

该变换替换任何NL字符在文本节点与br元件

<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文档(所提供的一个具有添加的施加cd使其更有趣):

<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> 

说明

  1. 身份规则/模板拷贝的每一个节点 “原样”。

  2. ,任何文本节点(也命名为“repNL”)匹配的首要模板prforms以下处理:

  3. 使用定点(NL字符附加到字符串)时,第一NL之前的子字符(或完整的字符串,如果不包含NL字符)复制到输出。

  4. 如果确实包含了NL字符,则会生成一个br元素,并且该模板会在该NL字符后递归调用其余字符串。

0

如果您正在使用XslCompiledTransform(C#),我相信你会遇到的问题,如果您使用以下API来转换XML:
XslCompiledTransform.Transform(XmlReader input, XmlWriter results)

但是,以下API效果很好:
XslCompiledTransform.Transform(string, string);
这是有线的,但我不明白,为什么...