需要将未关闭的元元素 HTML 转换为 XML

Need to convert the not closed meta element HTML to XML

我已经将 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.

使用命名模板开始您的代码,例如在 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 上,然后你的模板,如映射 htmldocument 应该可以工作,如果你使用

<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 默认为该模板.

为什么不用:

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

Xslt 处理器抛出错误,因为每个元素都必须有开始和结束标记。

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

的缩写