使用xsl创建段落
嘿,如果有人对如何从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文档保留段落结构的命令。
感谢, ē
看看FXSL 1.2,http://sourceforge.net/projects/fxsl/。我无法回答这个项目的质量和实用性,但至少它包含了很多东西和一些你可能需要的东西。
否则,攻击将选择主体的文本节点,并使用substring-before和substring-after函数递归创建新的文本节点,并用“p”节点围绕每个新的文本节点。递归位可能是棘手的部分,但上面提到的代码中有很多示例。
这种转变:
<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, '
'))">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:value-of select=
"substring-before($pText,'
')"/>
</p>
<xsl:call-template name="replaceNL">
<xsl:with-param name="pText" select=
"substring-after($pText,'
')"/>
</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
中。
谢谢你的帮助。它工作的很好,并且会帮助我更好地理解XSL。 – user633264 2011-04-01 22:23:08
@ user633264:很高兴我的解决方案“效果很好”,对您有用。在这里,这是正式的礼节,原来的海报预计会接受最好的答案。点击答案旁边的绿色复选标记即可轻松完成。 :) – 2011-04-01 22:56:51
@ user633264:最简单的方法是使用'xsl:appy-templates select =“body”'而不是'xsl:value-of',然后用新的行字符标记文本节点(记住这是标准化为' #xA;')添加'br'元素或包装到'p'元素中。这里有很多例子。 – 2011-03-31 16:04:26
好问题,+1。查看我的答案,获得完整,简短和简单的解决方案。 – 2011-04-01 13:40:52