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

问题描述

我已经尝试将HTML更改为XML,并且在HTML输入中具有未关闭Meta元素。

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

关闭Meta元素在Input中未显示任何验证错误,但是在进行转换时,出现以下错误

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中

 var tableData = [];
 $('.aantalNumber').change(function() {
   var aantalNumberVal = $(this).val()
   var Productnummer = $(this).closest('tr').find('.product_number').text();
   var Productnaam = $(this).closest('tr').find('.product_name').text();
   var verpakking = $(this).closest('tr').find('.verpakking').text();

   if (tableData.some(tableData => tableData.Productnummer === Productnummer)) {
     updateTableData(Productnummer,aantalNumberVal);
   } else {
     tableData.push({
       aantalNumber: aantalNumberVal,Productnummer: Productnummer,Productnaam: Productnaam,verpakking: verpakking
     });
   }


   console.log(tableData);
 });

 function updateTableData(value,aantalNumber) {
   for (var i in tableData) {
     if (tableData[i].Productnummer == value) {
       tableData[i].aantalNumber = aantalNumber;
       break; //Stop this loop,we found it!
     }
   }
 }

和选项<xsl:template name="main"> <xsl:copy-of select="saxon:parse-html(unparsed-text($unparse))"/> </xsl:template> 从命令行开始。那应该显示您从it:main方法获得的树及其默认序列化。

我认为默认情况下,它在XHTML名称空间中输出元素,而不是像HTML 4那样在没有名称空间中输出元素。因此,如果您要转换从parse-html返回的元素,则需要在该名称空间上进行匹配,例如parse-html上的xpath-default-namespace="http://www.w3.org/1999/xhtml",然后使用模板xsl:stylesheethtml的模板就可以使用

document

请注意,9.9支持XSLT 3,因此您可以使用<xsl:template name="main"> <xsl:apply-templates select="saxon:parse-html(unparsed-text($unparse))"/> </xsl:template> 而不是name="xsl:initial-template",并且省去了拼写初始模板名称的麻烦,因为选项name="main"默认为该模板。 / p>

,

为什么不使用:

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

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

- 在XHTML中,XML规则适用,因此每个元素无一例外必须同时具有开始标签和结束标签,但是如果元素内容为空(例如,简称。

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