Hey, I was wandering if anyone had any suggestions on how to take new-lines from an XML file and convert them to paragraphs with an XSL transform.
Here is a what the XML structure looks like:
<?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>
And here is a copy of the XSL sheet I'm using for the transform.
<?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>
Again, my question pertains to what commands to use to preserve the paragraph structure using the existing XSL document.
Thanks, E
This transformation:
<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>
when applied on the provided XML document:
<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>
produces the wanted, correct result:
<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>
Explanation: The identity rule + a recursive named template for wrapping into a p
each text substring surrounded by NL characters.
Take a look at FXSL 1.2, http://sourceforge.net/projects/fxsl/. I cannot answer to the quality and usefulness of this project, but at least it contains a lot of stuff and some that you might need.
Otherwise, the attack would be to select the text node of the body and recursively create new text nodes using the substring-before and substring-after functions and surrounding each new text node with a "p" node. The recursive bit is probably the tricky part, but there are lots of examples in the code mentioned above.
精彩评论