html - 仅从混合的 xml 和 HTML 中复制 HTML

标签 html xslt

我们有一堆文件是 html 页面,但其中包含额外的 xml 元素(都以我们的公司名称“TLA”为前缀)为我现在正在重写的旧程序提供数据和结构。

示例表格:

<html >
<head>
    <title>Highly Simplified Example Form</title>
</head>
<body>
    <TLA:document xmlns:TLA="http://www.tla.com">
        <TLA:contexts>
            <TLA:context id="id_1" value=""></TLA:context>
        </TLA:contexts>
        <TLA:page>
            <TLA:question id="q_id_1">
                <table>
                    <tr>
                        <td>
                            <input id="input_id_1" type="text" />
                        </td>
                    </tr>
                </table>
            </TLA:question>
        </TLA:page>
        <!-- Repeat many times -->
    </TLA:document>
</body>
</html>

我的任务是编写一个预处理器,它将只复制 html 元素,将它们的属性和内容完整地复制到一个新文件中。

像这样:

<html >
<head>
    <title>Highly Simplified Example Form</title>
</head>
<body>
    <table>
        <tr>
            <td>
                <input id="input_id_1" type="text" />
            </td>
        </tr>
    </table>
    <!-- Repeat many times -->
</body>
</html>

我采用了使用 XSLT 的方法,因为这是我需要的 extract the TLA elements对于不同的文件。到目前为止,这是我拥有的 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:mbl="http://www.mbl.com">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*" />
  <xsl:template match="mbl:* | mbl:*/@* | mbl:*/text()"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>    
</xsl:stylesheet>

但是这只会产生以下内容:

<html >
<head>
    <title>Highly Simplified Example Form</title>
</head>
<body>
</body>
</html>

如您所见,TLA:document 元素中的所有内容都被排除在外。需要在 XSLT 中更改什么才能获取所有 html 但过滤掉 TLA 元素?

或者,有没有更简单的方法来解决这个问题?我知道几乎每个浏览器都会忽略 TLA 元素,那么有没有办法使用 HTML 工具或应用程序获取我需要的内容?

最佳答案

专门针对 HTML 元素很难,但如果您只想从 TLA 命名空间中排除内容(但仍包括 TLA 元素包含的任何非 TLA 元素),那么这应该可行:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mbl="http://www.tla.com" exclude-result-prefixes="mbl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*" />

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

  <!-- This element-only identity template prevents the 
       TLA namespace declaration from being copied to the output -->
  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <!-- Pass processing on to child elements of TLA elements -->  
  <xsl:template match="mbl:*">
    <xsl:apply-templates select="*" />
  </xsl:template>
</xsl:stylesheet>

如果你想排除任何具有任何非空命名空间的东西,你也可以使用它:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mbl="http://www.tla.com" exclude-result-prefixes="mbl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*" />

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

  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="*[namespace-uri()]">
    <xsl:apply-templates select="*" />
  </xsl:template>
</xsl:stylesheet>

当对您的示例输入运行任何一个时,结果是:

<html>
  <head>
    <title>Highly Simplified Example Form</title>
  </head>
  <body>
    <table>
      <tr>
        <td>
          <input id="input_id_1" type="text" />
        </td>
      </tr>
    </table>
  </body>
</html>

关于html - 仅从混合的 xml 和 HTML 中复制 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15827349/

相关文章:

XSLT 前同级限制和解决方法

c# - XPath 显式索引过滤器性能

javascript - 如何将我的事件处理与 HTML 代码分开

javascript - 如何让 setInterval 在没有用户输入的情况下自动运行

html - Bootstrap 4 导航栏在 div 内打开

具有动态用户输入的 HTML5 Canvas

xml - 在 xslt 模板中创建 key

xml - 根据 XSL 参数按名称删除元素和/或属性

java - 如何定义正确的 XSLT?

javascript - 如何取消选中多个选定的选择器中的选项