2013-03-08 55 views
2

在給定的非常簡單,標準配置:如何刪除具有與使用XSLT給定的屬性值不同的節點?

<?xml version="1.0" encoding="utf-8" ?> 
<configuration xmlns:env="urn:schemas-test-env"> 
    <appSettings> 
     <add key="Name" value="Value" /> 
     <add key="Name" value="Value" env:name="Dev" /> 
     <add key="Name" value="Value" env:name="QA" /> 
    </appSettings> 

    <!-- rest of the config --> 

</configuration> 

我想用XSLT刪除所有節點<add />哪裏@env:name != $env?我的主要問題是將配置的其餘部分保持原樣。

我到目前爲止有:

<!-- populated by inline C# code --> 
<xsl:variable name="env" select="code:GetEnvironment()" /> 

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

<!-- Remove non-matching nodes --> 
<xsl:template match="configuration/appSettings/add"> 
    ??? 
</xsl:template> 

我有一個多個分支:

<xsl:choose> 
    <xsl:when test="not(@env:name)"> 
     <xsl:value-of select="'no env'" /> 
    </xsl:when> 
    <xsl:when test="./@env:name = $env"> 
     <xsl:value-of select="'Env eq var'" /> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="'Env neq var'" /> 
    </xsl:otherwise> 
</xsl:choose> 

回答

2

這種轉變

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:env="urn:schemas-test-env"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:param name="env" select="'QA'"/> 

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

<xsl:template match="add"> 
    <xsl:if test="not(@env:name = $env)"> 
    <xsl:call-template name="identity"/> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<configuration xmlns:env="urn:schemas-test-env"> 
    <appSettings> 
     <add key="Name" value="Value" /> 
     <add key="Name" value="Value" env:name="Dev" /> 
     <add key="Name" value="Value" env:name="QA" /> 
    </appSettings> 
    <!-- rest of the config --> 
</configuration> 

產生想要的,正確的結果:

<configuration xmlns:env="urn:schemas-test-env"> 
    <appSettings> 
     <add key="Name" value="Value"/> 
     <add key="Name" value="Value" env:name="Dev"/> 
    </appSettings><!-- rest of the config --> 
</configuration> 

說明:的identity rule

正確使用和壓倒一切的。


二, XSLT 2.0+溶液

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:env="urn:schemas-test-env"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:param name="env" select="'QA'"/> 

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

<xsl:template match="add[@env:name = $env]"/> 
</xsl:stylesheet> 

說明

XSLT從XSLT 1.0 2.0不同之處在於它允許變量引用作爲匹配圖案的謂詞的一部分。該功能使得只有一個空的覆蓋模板成爲可能。

+0

不幸的是,2.0在Visual Studio XSLT Debug中拋出異常「操作可能會破壞運行時」。 – abatishchev 2013-03-08 05:13:36

+0

我想實現一個相反的事情:當env = Dev時,只留下env-empty和env-equal給定,即「」和「Dev」。反之亦然。所以我使用'not(@env:name!= $ env)',它完美。謝謝! – abatishchev 2013-03-08 05:16:53

+0

[在線](http://www.xsltcake.com/slices/8B1eKo)2.0版本不起作用。你不知道爲什麼會發生? – abatishchev 2013-03-08 05:17:49

相關問題