如何使用XSLT破坏XML中的多行元素

时间:2014-10-15 14:33:47

标签: xml xslt

我有一个看起来像这样的XML文档

<ROOT>
 <SUMMARY>
   This is line 1
   this is line 2
 </SUMMARY>
 <STEPSBEFORE>
   this is step 1
   this is step 2
 </STEPSBEFORE>
</ROOT>

我的XSLT目前带回输出:

摘要

这是第1行这是第2行

STEPSBEOFRE

这是第1步,这是第2步

**这是我的XSL代码

                <xsl:stylesheet version="1.0"
                                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                                  <xsl:output omit-xml-declaration="yes"/>
                                  <xsl:strip-space elements="*"/>
                                    <xsl:template match="/">
                                    <xsl:comment>CHANGES TO THIS STRING WILL BE LOST - auto-generated by build process</xsl:comment>
                                      <html><body>
                                          <ROOT>
                                              <h2><b>Summary</b></h2>
                                              <xsl:value-of select="//Summary"/>
                                              <h2><b>StepsBefore</b></h2>                                       
                                               <xsl:value-of select="//StepsBefore"/>
                                               <xsl:for-each select="//StepsBefore">
                                                <xsl:value-of select="current()"/>
                                                <xsl:text>&#xA;</xsl:text>
                                               </xsl:for-each>
                                          </ROOT>
                                          </body></html>
                                  </xsl:template>
                              </xsl:stylesheet>

我希望每行显示输出,就像在原始XML文件中一样;但这不起作用......任何想法我怎么能达到我所追求的每线影响?理想情况下每个都有一个子弹点。

2 个答案:

答案 0 :(得分:2)

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates select="ROOT/*"/>
        </body>
    </html>
</xsl:template>

<xsl:template match="*">
    <h2>
        <xsl:value-of select="local-name()"/>
    </h2>
    <ul>
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </ul>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'&#10;'"/>
        <xsl:variable name="token" select="normalize-space(substring-before(concat($text, $delimiter), $delimiter))" />
        <xsl:if test="$token">
            <li>
                <xsl:value-of select="$token"/>
            </li>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例时,将返回:

<html>
   <body>
      <h2>SUMMARY</h2>
      <ul>
         <li>This is line 1</li>
         <li>this is line 2</li>
      </ul>
      <h2>STEPSBEFORE</h2>
      <ul>
         <li>this is step 1</li>
         <li>this is step 2</li>
      </ul>
   </body>
</html>

呈现为:

enter image description here

答案 1 :(得分:0)

如果您想将XML转换为HTML并希望按原样渲染空白区域,请使用HTML pre元素,例如

<xsl:template match="SUMMARY">
  <h2>Summary</h2>
  <pre><xsl:apply-templates/></pre>
</xsl:template>

然后确保与/匹配的模板<xsl:apply-templates/>