【问题标题】:Retain formatting tags from XML with XSLT使用 XSLT 从 XML 中保留格式标记
【发布时间】:2015-06-30 19:00:59
【问题描述】:

我必须使用 XSLT 显示 XML 文档,但我想应用原始 XML 文档中的 <p><i> HTML 标记。例如,来自 XML 的 p 标记将创建一个新段落。实现这一目标的最佳方法是什么? XML 和 XSLT 代码贴在下面。

编辑:澄清原始问题

<?xml version="1.0" encoding="UTF-8" ?> 
<?xml-stylesheet type="text/xsl" href="tour.xsl"?>
<cars>
   <car>
      <description>
        <p><i>There is some text here</i> There is some more text here</p>
        <p>This should be another paragraph</p>
        <p>This is yet another paragraph</p>
      </description>
   </car>
</cars>

<?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0"        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="4.0"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Title</title>
            </head>
            <body>
                <xsl:value-of select="car"/><p/>       
            </body>
        </html>
    </xsl:template>
 </xsl:stylesheet>`

【问题讨论】:

  • 感谢您的回复。有没有办法应用这些标签,以便“i”标签之间的文本以斜体书写,“p”标签开始一个新段落?

标签: html xml xslt tags formatting


【解决方案1】:

您可以扩展模板以复制 p 元素,如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="4.0"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>Title</title>
      </head>
      <body>
        <xsl:for-each select="/cars/car">
          <xsl:for-each select="description/p">
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

根据您的 XML 输入,上述 XSLT 将产生以下输出:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Title</title>
   </head>
   <body>
      <p><i>There is some text here</i> There is some more text here
      </p>
      <p>This should be another paragraph</p>
      <p>This is yet another paragraph</p>
   </body>
</html>

更新:

我正在尝试在 komodo 的内置浏览器和 ie 中运行。

您必须研究 komodo 的 XSLT 功能。另外,请注意challenges running XSLT in the browser;最好在服务器上或批处理模式下运行,并且只使用浏览器显示结果。

也就是说,以下 XML 文件将在 IE 11 中打开,并根据上述 XSLT 表设置样式(命名为 tour.xml,与 XML 文件位于同一目录中):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='tour.xsl'?>
<cars>
   <car>
      <description>
        <p><i>There is some text here</i> There is some more text here</p>
        <p>This should be another paragraph</p>
        <p>This is yet another paragraph</p>
      </description>
   </car>
</cars>

这是 IE 11 中输出的屏幕截图:


更新 2

我尝试了模板解决方案,但它仍然返回未格式化的文本 一行。

如果给 IE 11 一个没有 xml-stylesheet PI 的 XML 文件,

<?xml-stylesheet type='text/xsl' href='tour.xsl'?>

然后它将以大纲形式显示 XML。

如果给 IE 11 一个带有 xml-stylesheet PI 的 XML 文件并且可以找到指定的 XSLT 文件,它将适用XSLT 并在浏览器中显示结果。此结果可以在上面的屏幕截图中看到(并且是您想要的结果)。

如果给 IE 11 一个带有 xml-stylesheet PI 的 XML 文件并且无法找到指定的 XSLT 文件,它将显示正如您所描述的那样,一行中的无格式文本。

因此,将搜索问题的重点放在 XSLT 文件的位置和名称上。 当我通过重命名 XSLT 文件来故意强制执行此类行为时,会发生以下情况,

<?xml-stylesheet type='text/xsl' href='tour_CANNOT_FIND.xsl'?>

这样就找不到了:

注意:按 F-12 显示控制台。

然后,如果您点击“允许阻止的内容”按钮:

您会看到无法找到tour_CANNOT_FIND.xsl 的诊断消息。 如果您解决了 IE 找不到您的 XSLT 文件的问题,您应该会在浏览器中看到您的 XSLT 文件的格式化结果。

【讨论】:

  • 两种方式都试过了,但由于某种原因没有应用格式。还有其他建议吗?我可能会遗漏其他内容,抱歉我是 XML 新手。
  • 您想在浏览器中运行 XSLT 还是通过独立处理器运行?
  • 我正在尝试在 komodo 的内置浏览器中运行,即
  • 再次感谢您的回复。我尝试了模板解决方案,但它仍然在一行中返回未格式化的文本。上面的 IE 屏幕截图正是我想要实现的,但是在尝试了 komodo 和 IE 之后,我得到了未格式化的文本。如果您有任何可能的解决方案,请发布。
  • 当 IE 找不到 XSLT 文件时会出现无格式文本。答案更新了更多细节。
【解决方案2】:

你的实际问题可以用这样的样式表来回答,它使用apply-templates

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

    <xsl:template match="/">
        <html>
            <head>
                <title>Title</title>
            </head>
            <body>
                <xsl:apply-templates select="/cars/car/description/*"/>
                <p/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
 </xsl:stylesheet>

但是,如果您有多个cars,您可能希望有一些头条新闻。然后像这样的样式表可能指向那个目标:

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

    <xsl:template match="/">
        <html>
            <head>
                <title>Title</title>
            </head>
            <body>
                <xsl:apply-templates select="/cars/car/*"/>
                <p/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="description">
        <h1>Subtitle</h1>
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
 </xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多