Xml 使用xsl创建段落

Xml 使用xsl创建段落,xml,xslt,newline,transform,paragraph,Xml,Xslt,Newline,Transform,Paragraph,嘿,我想知道是否有人对如何从XML文件中获取新行并使用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;

嘿,我想知道是否有人对如何从XML文件中获取新行并使用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>
下面是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>

1.
“老兄,我真不敢相信你把它喂给了你的猫。真是疯了!”
“是的,哥们,他就抱在我旁边,开始咕噜咕噜。”
“那么他做了什么?”
“他一直在咕噜咕噜,伙计。他已经连续咕噜了两个星期了。我甚至睡不着。”
这是我用于转换的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

看看FXSL 1.2。我无法回答这个项目的质量和有用性,但至少它包含了很多东西和一些你可能需要的东西

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

请看FXSL 1.2。我无法回答这个项目的质量和有用性,但至少它包含了很多东西和一些你可能需要的东西


否则,攻击将是选择正文的文本节点,并使用函数前的子字符串和函数后的子字符串递归地创建新文本节点,并用“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, '&#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>
<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>


应用于提供的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>

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>
<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>

1.

“老兄,我真不敢相信你把它喂给了你的猫。真是疯了!”

“是的,哥们,他就抱在我旁边,开始咕噜咕噜。”

“那他做了什么?”“他只是不停地咕噜,伙计。他不停地咕噜了两个星期。我甚至睡不着。”


解释:标识规则+一个递归命名模板,用于将每个文本子字符串包装成一个由NL字符包围的
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, '&#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>
<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>


应用于提供的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>

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>
<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>

1.

“老兄,我真不敢相信你把它喂给了你的猫。真是疯了!”

“是的,哥们,他就抱在我旁边,开始咕噜咕噜。”

“那他做了什么?”“他只是不停地咕噜,伙计。他不停地咕噜了两个星期。我甚至睡不着。”


解释:标识规则+一个递归命名模板,用于包装到由NL字符包围的每个文本子字符串中。

@user633264:最简单的方法是使用
xsl:appy templates select=“body”
而不是
xsl:value of
,然后用新行字符标记文本节点(请记住,这被规范化为
#xA;
)添加
br
元素或包装成
p
元素。这里有很多示例。好问题,+1。请参阅我的答案,以获得完整、简短和简单的解决方案。@user633264:最简单的方法是使用
xsl:appy templates select=“body”
而不是
xsl:value of
,然后通过新行字符标记文本节点(请记住,这已规范化为
#xA;
)添加
br
元素或包装成
p
元素。这里有很多示例。好问题,+1。请参阅我的答案,以获得完整、简短且简单的解决方案。感谢您的帮助。它非常有效,将帮助我更好地理解XSL。@user633264:很高兴我的解决方案“非常有效”并且对您很有用。这里是SO官方网站,希望原始海报能够接受最佳答案。点击答案旁边的绿色复选标记即可轻松完成。谢谢您的帮助。它工作得很好,将帮助我更好地理解XSL。@user633264:很高兴我的解决方案“工作得很好”,对您很有用。在这里,这是官方的ettiquette,原海报预计会接受最好的答案。单击答案旁边的绿色复选标记即可轻松完成此操作。:)