【发布时间】:2010-11-02 14:51:22
【问题描述】:
我有一个 XML 文档,其中包含不同级别的附加节点,我想通过 XSLT 删除这些节点。
我的直觉是生成一个 XSD(使用 Altova 等),删除我不想出现在输出中的元素,然后让编辑器(Altova 等)自动生成一个 XSLT 来转换旧的XSD 转换为新的 XSD。
过去,我曾经为此手动编写 XSL...但是使用所有这些很酷的工具,真的有理由或有人可以提出替代方法吗?我已经有几年没做过这些东西了,所以我想我会问...
我最终想出了这个,这很简单,所以谢谢你的建议:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" >
<xsl:output method="xml" indent="yes"/>
<xsl:template xpath-default-namespace="http://www.tempuri.org" match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template xpath-default-namespace="http://www.tempuri.org" match="SomeNode">
</xsl:template>
<xsl:template xpath-default-namespace="http://www.tempuri.org" match="TheNode[@type!='SomeType' and @type!='OtherType']">
</xsl:template>
</xsl:stylesheet>
我需要做的另一件事是检查“TheNode”的缺失元素“SomeSubElement”,如果缺失则插入一个空元素。
关于如何做到这一点的任何建议?谢谢。
【问题讨论】:
-
就原因而言,编写 XSLT 可能比编写 XSD 更快...
-
首先,这是另一个问题。你应该再问一次。其次,这不是处理 QName 的方法:在
stylesheet根元素中只放置一个xpath-default-namespace="http://www.tempuri.org",或者在stylesheet根元素中放置一个命名空间声明xmlns:temp="http://www.tempuri.org",并在以下模式中使用完整的 QNametemp:SomeNode