【问题标题】:Create paragraphs with xsl使用 xsl 创建段落
【发布时间】:2011-03-31 14:55:01
【问题描述】:

嘿,如果有人对如何从 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 文档来保留段落结构。

谢谢, E

【问题讨论】:

  • @user633264:最简单的方法是使用xsl:appy-templates select="body"而不是xsl:value-of,然后用换行符标记文本节点(记住这被规范化为#xA;)添加br元素或包装成p 元素。这里有很多例子。
  • 好问题,+1。请参阅我的答案以获得完整、简短且简单的解决方案。

标签: xml xslt newline transform paragraph


【解决方案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

【讨论】:

  • 感谢您的帮助。它很好用,可以帮助我更好地理解 XSL。
  • @user633264:很高兴我的解决方案“效果很好”并且对您有用。在SO这里,官方礼仪是希望原始发布者接受最佳答案。这可以通过单击答案旁边的绿色复选标记轻松完成。 :)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2022-01-10
    相关资源
    最近更新 更多