2015-06-18 69 views
0

我在下面有XML。XSL - 帶有屬性的複製元素並排除特定的子元素

<rootElement rootAttribute1="value1" rootAttribute2="value2"> 
    <childElement childElementAttribute="value"> 
     <grandChild>First</grandChild> 
    </childElement> 
    <childElement childElementAttribute="copyMe"> 
     <grandChild>Second</grandChild> 
    </childElement> 
    <childElement childElementAttribute="value"> 
     <grandChild>Third</grandChild> 
    </childElement> 
</rootElement> 

我需要通過XSL處理它,將在下reules: 1. rootElement的元素應該與它的所有複製屬性 2. rootElement的元素,這是剛纔複製,應該包含有childElementAttribute僅是childElement元素=「copyMe」(在這種情況下,比較「copyMe」的字符串,但是它是以二進制方式生成的)(及其所有屬性)。 「copyMe」這裏只是特定的值,但 所以,上面的例子應該在未來一

<rootElement rootAttribute1="value1" rootAttribute2="value2"> 
    <childElement childElementAttribute="copyMe"> 
     <grandChild>Second</grandChild> 
    </childElement> 
</rootElement> 

轉變這裏是我試過

的XSL
<!-- Copy rootElement element--> 
<xsl:template match="node()|@*" mode="copyAndExclude"> 
    <xsl:param name="requiredAttrivuteValue"/> 
    <xsl:if test="childElement[@childElementAttribute=$requiredAttrivuteValue]" > 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" mode="excludeUnnecessaryChilds" /> 
     </xsl:copy> 
    </xsl:if> 
</xsl:template> 

<!-- exclude unnecessary child elements--> 
<xsl:template match="node()|@*" mode="excludeUnnecessaryChilds"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*" mode="excludeUnnecessaryChilds" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/"> 
    .... 
    <xsl:apply-templates select="rootElement" mode="copyAndExclude" > 
     <xsl:with-param name="requiredAttrivuteValue" select="$someValue"/> 
    </xsl:apply-templates> 
<xsl:template> 

Here someValue is generated dinamically and depends on few things, that are not significant here 

預先感謝您!

+2

到複製請張貼XSLT的元素你已經嘗試過。謝謝。 – potame

回答

3

我不認爲你需要的模式,簡單地用

<xsl:template match="@* | node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
</xsl:template> 

開始然後排除你不想

<xsl:template match="childElement[not(@childElementAttribute = 'copyMe')]"/> 
+0

你好,謝謝你的回覆。模式在這裏是必需的,因爲我發佈的XML只是所有文檔的一部分。所以我需要確保該模板不會應用於其他元素。 – Bogdan

+0

另外,我需要從一個變量中退出soveValue。 我更新了有問題的信息。謝謝! – Bogdan

相關問題