我正在学习XSLT,并试图过滤现有的XML文件,并且基本上只复制具有感兴趣的选定节点的树 - 如果它们匹配条件,比如说有子串“测试”。如何在XSL中筛选?
使用恒等变换模式,我有以下片段:
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="(node() | @*)[contains(name(.), 'test')]" />
<!-- Bonus Q: What would be happening behind the scenes when I put this code inside the copy element?
<xsl:value-of select="." /> -->
</xsl:copy>
</xsl:template>
现在,这个让我下面的输出,
<head>
<testA> <testAChild /></testA> <!-- should be <testA> <testAChild> foo </testAChild></testA> -->
<testB /> <!-- should be <testB> bar </testB> -->
<Ctest /> <!-- should be <Ctest> foobar </Ctest> -->
<!-- should be <DontDeleteThis> <testD> 420 </testD></DontDeleteThis> -->
</head>
但是,它并没有在结果中的值树。此外,它不检查每个情况,当前节点是错误的,但是儿童,孙辈或可能是盛大的孩子可以通过标准。
下面是输入XML的例子:
<head>
<testA>
<testAChild> foo </testAChild>
</testA>
<testB> bar </testB>
<Ctest> foobar </Ctest>
<DeleteThis> DELETED </DeleteThis>
<DontDeleteThis>
<testD> 420 </testD>
</DontDeleteThis>
</head>
请提供您的XML文件。 – AntonH
@AntonH我编辑了这个问题,并用XML片段进行了更新。我正在寻找这种问题的通用解决方案,因此它应该足够灵活以适应其他类型的XML结构。 – WCGPR0