【问题标题】:XML encoded HTML to XSLTXML 编码的 HTML 到 XSLT
【发布时间】:2016-03-03 17:04:18
【问题描述】:

如何获取 XML 编码的 HTML 并创建 XSLT?我有链接到 XSLT 文件的 xml/html 页面,它显示文档中的文本,但不会显示链接或图片。 XML/HTML 中的图像位于 xml 和 xslt 所在文件夹中名为 images 的文件夹中。

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="XLST.xslt"?>

  <html>
    <head>
      <title>CATS! CATS! CATS!</title>
    </head>
    <body>
      <h1>CATS</h1>

      <p>
        <a href=" www.google.com">Visit Google...</a>
      </p>

      Cats like milk!
      <p>
        <image> <img src="Cats.jpg" alt="Cats, so cute!"/></image>
      </p>
    </body>
  </html>

XSLT 文件:

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="html">
    <xsl:apply-templates select="body" />


  </xsl:template>

  <xsl:template match="body">
    <img alt="">
      <xsl:attribute name="src">
        <xsl:value-of select="//Cats"/>
      </xsl:attribute>
    </img>
  </xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 您的输入已经是 HTML - 为什么需要对其进行转换? --- 请注意,&lt;xsl:value-of select="//Cats"/&gt; 不会返回任何内容,因为在您的输入中的任何位置都没有名为 Cats 的元素。
  • 因为我想知道怎么做。
  • 这很令人钦佩,但是如果不知道这里的目的是什么,很难帮助您。例如,您可以简单地使用&lt;xsl:copy-of select="//img"/&gt;(作为匹配body 的模板的全部内容)。
  • 在开始编写 XSLT 之前,您应该知道您想要什么输出。在这种情况下,您能否显示您实际期望的输出?谢谢!

标签: html xml xslt


【解决方案1】:

将现有的 xhtml 文档转换为几乎相同的 xhtml 文档,只需添加一些内容,这不是您通常会做的事情,但它相对容易。从只包含标识模板的样式表开始:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

这将完全按原样输出 xhtml,然后您可以添加模板来补充您的源 xhtml。例如,添加:

<xsl:template match="body">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <img alt="" src="(insert url here)"/>
    <xsl:apply-templates select="node()"/>
  </xsl:copy>
</xsl:template>

将在body 元素的开头添加一个图像。它复制元素本身,然后是属性,插入图像,然后复制子元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多