我正在學習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