【问题标题】:Need to convert the not closed meta element HTML to XML需要将未关闭的元元素 HTML 转换为 XML
【发布时间】:2020-12-06 12:24:45
【问题描述】:

我一直致力于将 HTML 更改为 XML,我在 HTML 输入中有未关闭的 Meta 元素。

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Sample</title>
   </head>
</html>

未关闭的元元素在输入中未显示任何验证错误,但在进行转换时出现以下错误:

The element type "meta" must be terminated by the matching end-tag "</meta>"

XSL 我试过了:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    xmlns:saxon="http://saxon.sf.net/"
    version="2.0">

      <xsl:template match="html">
       <document>
          <xsl:apply-templates/>
        </document>
      </xsl:template>

  <xsl:template match="head">
    <head>
      <xsl:apply-templates/>
    </head>
  </xsl:template>

  <xsl:template match="title">
    <title>
      <xsl:apply-templates/>
    </title>
  </xsl:template>

  <xsl:param name="unparse" select="'file:///C:test.htm'"/>

  <xsl:template match="saxon:meta">
    <xsl:value-of select="saxon:parse-html($unparse)"/>
  </xsl:template>

</xsl:stylesheet>

我已经在 XSLT 中尝试了saxon:parse-html,但我无法转换。所以我需要使用 XSLT 删除未关闭的 Meta 元素。我正在使用 saxon-PE 9.9.1.5。

【问题讨论】:

  • 不确定您希望与 match="saxon:meta" 匹配的元素。我认为它不匹配任何东西,因此永远不会调用 parse-html()。

标签: xml xslt xslt-2.0 saxon


【解决方案1】:

使用命名模板开始您的代码,例如在 XSLT 中

<xsl:template name="main">
  <xsl:copy-of select="saxon:parse-html(unparsed-text($unparse))"/>
</xsl:template>

和来自命令行的选项it:main。这应该会显示您从 parse-html 方法获得的树及其默认序列化。

我认为默认情况下它会在 XHTML 命名空间中输出元素,而不是像 HTML 4 那样在任何命名空间中输出元素。因此,如果您想转换从 parse-html 返回的元素,您将需要匹配该命名空间,例如xpath-default-namespace="http://www.w3.org/1999/xhtml" 在您的 xsl:stylesheet 上,然后您的模板(例如将 html 映射到 document 的模板应该可以使用)

<xsl:template name="main">
  <xsl:apply-templates select="saxon:parse-html(unparsed-text($unparse))"/>
</xsl:template>

请注意,9.9 支持 XSLT 3,因此您可以使用 name="xsl:initial-template" 而不是 name="main",并且不必拼写初始模板的名称,因为选项 -it 默认为该模板。

【讨论】:

    【解决方案2】:

    为什么不使用:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    

    Xslt 处理器抛出一个错误,因为每个元素都必须有一个打开和关闭标记。

    -- 在 XHTML 中,XML 规则适用,因此每个元素无一例外都必须同时具有开始标记和结束标记,但如果元素内容为空,则可以将相同的标记用于两个角色,例如的缩写。

    https://stackoverflow.com/a/19510239/3692798

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2020-12-03
      • 1970-01-01
      • 2011-10-30
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      相关资源
      最近更新 更多