复制命名空间的所有元素,仅此而已 [英] Copy all elements of a namespace and nothing else

查看:22
本文介绍了复制命名空间的所有元素,仅此而已的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a bunch of files that are html pages but which contain additional xml elements (all prefixed with our company name 'TLA') to provide data and structure for an older program which I am now rewriting.

示例表单:

<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>

我的任务是编写一个预处理器来提取所有TLA"元素并忽略 html 元素

My task is to write a pre-processor that will extract all the 'TLA' elements and ignore the html elements

所需的 XML 输出:

Desired XML Output:

<?xml version="1.0" encoding="utf-8" ?>
<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">
      </TLA:question>
    </TLA:page>
    <!-- Repeat many times -->
</TLA:document>

这对于 XSLT 应该是可行的,但我无法制定正确的代码.这是我目前所拥有的:

This should be doable with XSLT but I'm unable to formulate the correct code. This is what I have so far:

<?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:tla="http://www.tla.com"
>
    <xsl:output method="xml" indent="yes"/>

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

这是提取我想要的元素(但不是它们的属性!)而且还提取了 html 元素的文本属性和内容.如何排除 html 元素及其内容?

Which is extracting the elements I want (but not their attributes!) but also extracts the text attributes and content of the html elements. How can I exclude the html elements and their content?

推荐答案

应该这样做:

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

  <xsl:template match="text()" />

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

在您的示例输入上运行时(一旦添加了缺少的命名空间声明),结果是:

When run on your sample input (once the missing namespace declaration is added), the result is:

<TLA:document xmlns:TLA="http://www.tla.com">
  <TLA:contexts>
    <TLA:context id="id_1" value="" />
  </TLA:contexts>
  <TLA:page>
    <TLA:question id="q_id_1" />
  </TLA:page>
</TLA:document>

这篇关于复制命名空间的所有元素,仅此而已的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆