2013-07-16 57 views
0

使用XLST 1.0我需要檢索AA元素在它不具有「過濾我出去」 或「和過濾我BB元素也是'。獲取父元素,其中有特定值的子元素不存在

<data> 
    <aa> 
     <bb>Filter me out</bb> 
     <bb>Some information</bb> 
    </aa> 
    <aa> 
     <bb>And filter me out too</bb> 
     <bb>Some more information</bb> 
    </aa> 
    <aa> 
     <bb>But, I need this information</bb> 
     <bb>And I need this information</bb> 
    </aa> 
</data> 

一旦我有正確的AA元素我會輸出它的每一個BB元素,像這樣:

<notes> 
    <note>But, I need this information</note> 
    <note>And I need this information</note> 
</notes> 

非常感謝。

回答

2

的標準方法來這樣的事情是使用模板

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <!-- copy everything as-is from input to output unless I say otherwise --> 
    <xsl:template match="@*|node()"> 
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy> 
    </xsl:template> 

    <!-- rename aa to notes --> 
    <xsl:template match="aa"> 
    <notes><xsl:apply-templates select="@*|node()" /></notes> 
    </xsl:template> 

    <!-- and bb to note --> 
    <xsl:template match="bb"> 
    <note><xsl:apply-templates select="@*|node()" /></note> 
    </xsl:template> 

    <!-- and filter out certain aa elements --> 
    <xsl:template match="aa[bb = 'Filter me out']" /> 
    <xsl:template match="aa[bb = 'And filter me out too']" /> 
</xsl:stylesheet> 

這些最後兩個模板匹配特定aa元素,你希望,然後什麼也不做。與特定過濾模板不匹配的任何aa元素將與較不具體的<xsl:template match="aa">匹配,並且將其重命名爲notes

任何沒有特定模板的東西都會被第一個「標識」模板所捕獲,並被複制到輸出中。這包括包含所有aa元素(您沒有在您的示例中提供但它必須存在或輸入不是格式良好的XML)的父元素。

+0

非常感謝你伊恩! – nrg

相關問題