繁体   English   中英

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

[英]How do i break multi-line element in my XML using 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

**这里是我的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文件中一样; 但这不起作用...任何想法我如何才能实现每行影响我之后? 理想情况下,每个都有一个要点。

以下样式表:

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>

呈现为:

在此处输入图片说明

如果要将XML转换为HTML,并希望按原样呈现空白,请使用HTML pre元素,例如

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM