复制命名空间的所有元素,仅此而已

编程入门 行业动态 更新时间:2024-10-20 00:48:58
本文介绍了复制命名空间的所有元素,仅此而已的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一堆文件是 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="www.tla"> <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="www.tla"> <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="www.w3/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:tla="www.tla" > <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="www.w3/1999/XSL/Transform" xmlns:tla="www.tla"> <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="www.tla"> <TLA:contexts> <TLA:context id="id_1" value="" /> </TLA:contexts> <TLA:page> <TLA:question id="q_id_1" /> </TLA:page> </TLA:document>

更多推荐

复制命名空间的所有元素,仅此而已

本文发布于:2023-07-05 06:58:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:仅此而已   元素   空间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!