【发布时间】:2014-05-20 16:17:55
【问题描述】:
假设我有以下 XML 输入文件文件:
<root>
<NodeX>
...
</NodeX>
<NodeY>
...
</NodeY>
<Description>
<section01>
<subsection key="KeyA">Some text</subsection>
<subsection key="KeyB">Some text</subsection>
</section1>
<section02>
<subsection key="KeyC">Some text</subsection>
</section2>
<section03>
<subsection key="KeyD">Some Text</subsection>
</section3>
</Description>
...
</root>
还有另一个带有“规则”的 XML 文件,其中子部分的键与受众属性一起列出。
示例摘录:
<rules>
<subsection id="01">
<key audience="internalOnly">KeyA</key>
<key audience="internalOnly">KeyB</key>
</subsection>
<subsection id="02">
<key>KeyC</key>
</subsection>
<rules>
我尝试编写一个 XSL 转换,它根据规则 XML 填充中的受众属性值从输入 XML 文件中删除子部分。如果值为“internalOnly”,则必须删除该小节。
在示例中,应生成以下输出 XML:
<root>
<NodeX>
...
</NodeX>
<NodeY>
...
</NodeY>
<Description>
<section02>
<subsection key="KeyC">Some text</subsection>
</section2>
<section03>
<subsection key="KeyD">Some Text</subsection>
</section3>
</Description>
...
</root>
(整个 section01 被删除,因为两个小节都是“internalOnly”)。
问题是 1) 规则 XML 文件中的“查找”和 2) 如果删除了所有相应的子节,则删除节元素:
如果观众属性将包含在输入 XML 文件中,我将为需求创建以下 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="subsection[@audience='internalOnly']"></xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
不要忘记添加您当前的 XSL... 以及您遇到的问题的详细信息。
-
这与您之前的问题有何不同:stackoverflow.com/questions/22663044/rule-based-restructuring-of-nodes/
-
我之前的问题中节点的重新排列是这里删除过程的某种先决条件。我想将删除步骤与重排步骤分开。我尝试根据您之前的答案编写 XSL 转换:stackoverflow.com/questions/22663044/rule-based-restructuring-of-nodes/ 但在理解 XSLT 键函数的用法时遇到了问题。 XSLT 键功能的使用是否适合我需要在此处删除的查找?
标签: xslt