我们有一堆 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>
我的任务是编写一个预处理器,它将提取所有“TLA”元素并忽略 html 元素
所需的 XML 输出:
<?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 实现,但我无法制定正确的代码。这是我到目前为止所拥有的:
<?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 元素及其内容?
这应该可以做到:
<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>
当在示例输入上运行时(添加缺少的命名空间声明后),结果为:
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)