2011-05-15 42 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);
這是有線的,但我不明白,爲什麼...