我已经将HTML更改为XML,我在HTML输入中使用了非闭合的Meta元素。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample</title>
</head>
</html>未关闭的meta元素在输入中没有显示任何验证错误,但在进行转换时,我得到了以下错误:
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。
发布于 2020-08-17 20:29:50
使用命名模板开始代码,例如在XSLT中
<xsl:template name="main">
<xsl:copy-of select="saxon:parse-html(unparsed-text($unparse))"/>
</xsl:template>和命令行中的it:main选项。这应该会向您显示树及其从parse-html方法获得的默认序列化。
我认为它默认输出XHTML名称空间中的元素,而不是像HTML4那样没有名称空间中的元素。因此,如果您想要转换从parse-html返回的元素,您将需要匹配该名称空间,例如xsl:stylesheet上的xpath-default-namespace="http://www.w3.org/1999/xhtml",然后您的模板(如将html映射到document的模板)应该可以使用
<xsl:template name="main">
<xsl:apply-templates select="saxon:parse-html(unparsed-text($unparse))"/>
</xsl:template>请注意,9.9支持XSLT3,因此您可以使用name="xsl:initial-template"而不是name="main",而不必拼写初始模板的名称,因为选项-it默认为该模板。
发布于 2020-08-17 21:19:18
为什么不使用:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> Xslt处理器抛出一个错误,因为每个元素都必须有一个开始和结束标记。
--在XHTML中,XML规则适用,因此每个元素都必须同时具有开始标记和结束标记,但如果元素内容为空,则两个角色可以使用相同的标记。
https://stackoverflow.com/questions/63449623
复制相似问题