使用xsl创建段落

时间:2011-03-31 14:55:02

标签: xml xslt newline transform paragraph

嘿,如果有人对如何从XML文件中获取新行并将它们转换为带有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文档保留段落结构的命令。

谢谢, ë

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

看看FXSL 1.2,http://sourceforge.net/projects/fxsl/。我无法回答这个项目的质量和实用性,但至少它包含了许多你可能需要的东西。

否则,攻击将是选择正文的文本节点,并使用substring-before和substring-after函数递归创建新的文本节点,并使用“p”节点围绕每个新文本节点。递归位可能是棘手的部分,但上面提到的代码中有很多例子。