2011-03-31 67 views
1

嘿,如果有人對如何從XML文件中取出換行符並將它們轉換爲帶有XSL轉換的段落有任何建議,我都會徘徊。使用xsl創建段落

這裏是一個XML結構的樣子:

<?xml version="1.0" encoding="ISO-8859-1"?> 

<document> 
<book> 
<issue>1</issue> 
<body> 
「Dude, I can't believe you fed it to your cat. That's crazy!」 

「Yeah, dude, he just cuddled up next to me and started purring.」 

「Then what did he do?」 

「He just kept purring, man. He's been purring non-stop for like two weeks now. I can't even sleep.」 
</body> 
</book> 
</document> 

這裏是我使用的變換XSL表的副本。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> 

<body style="font-family:Arial;font-size:12pt;"> 

<xsl:for-each select="document/book"> 

<div style="color:red; padding:4px;"> 
<span style="font-weight:bold"> 
</span> Chapter 
<xsl:value-of select="info/issue"/> 
</div> 
<div style="margin-left:10px; margin-bottom:1em; margin-right:25px; font-size:10pt;"> 
<span> 
<xsl:value-of select="body"/> 
</span> 
</div> 

</xsl:for-each> 
</body> 
</html> 

同樣,我的問題涉及使用現有的XSL文檔保留段落結構的命令。

感謝, ē

+0

@ user633264:最簡單的方法是使用'xsl:appy-templates select =「body」'而不是'xsl:value-of',然後用新的行字符標記文本節點(記住這是標準化爲' #xA;')添加'br'元素或包裝到'p'元素中。這裏有很多例子。 – 2011-03-31 16:04:26

+0

好問題,+1。查看我的答案,獲得完整,簡短和簡單的解決方案。 – 2011-04-01 13:40:52

回答

0

看看FXSL 1.2,http://sourceforge.net/projects/fxsl/。我無法回答這個項目的質量和實用性,但至少它包含了很多東西和一些你可能需要的東西。

否則,攻擊將選擇主體的文本節點,並使用substring-before和substring-after函數遞歸創建新的文本節點,並用「p」節點圍繞每個新的文本節點。遞歸位可能是棘手的部分,但上面提到的代碼中有很多示例。

1

這種轉變

<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="body/text()" name="replaceNL"> 
    <xsl:param name="pText" select="."/> 

    <xsl:if test="string-length($pText)"> 
    <xsl:choose> 
    <xsl:when test="not(contains($pText, '&#xA;'))"> 
     <xsl:value-of select="$pText"/> 
    </xsl:when> 
    <xsl:otherwise> 
    <p> 
     <xsl:value-of select= 
     "substring-before($pText,'&#xA;')"/> 
    </p> 
    <xsl:call-template name="replaceNL"> 
     <xsl:with-param name="pText" select= 
     "substring-after($pText,'&#xA;')"/> 
    </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<document> 
<book> 
<issue>1</issue> 
<body> 
「Dude, I can't believe you fed it to your cat. That's crazy!」 

「Yeah, dude, he just cuddled up next to me and started purring.」 

「Then what did he do?」 「He just kept purring, man. He's been purring non-stop for like two weeks now. I can't even sleep.」 
</body> 
</book> 
</document> 

產生想要的,正確的結果

<document> 
    <book> 
     <issue>1</issue> 
     <body> 
     <p/> 
     <p>「Dude, I can't believe you fed it to your cat. That's crazy!」</p> 
     <p>  </p> 
     <p>「Yeah, dude, he just cuddled up next to me and started purring.」</p> 
     <p>  </p> 
     <p>「Then what did he do?」 「He just kept purring, man. He's been purring non-stop for like two weeks now. I can't even sleep.」</p> 
     </body> 
    </book> 
</document> 

說明:身份規則+一個遞歸命名模板,用於包裝到由NL字符包圍的每個文本子字符串的p中。

+0

謝謝你的幫助。它工作的很好,並且會幫助我更好地理解XSL。 – user633264 2011-04-01 22:23:08

+0

@ user633264:很高興我的解決方案「效果很好」,對您有用。在這裏,這是正式的禮節,原來的海報預計會接受最好的答案。點擊答案旁邊的綠色複選標記即可輕鬆完成。 :) – 2011-04-01 22:56:51