Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何添加<;配置部分>;使用xslt访问web.config_Xml_Xslt_Web Config - Fatal编程技术网

Xml 如何添加<;配置部分>;使用xslt访问web.config

Xml 如何添加<;配置部分>;使用xslt访问web.config,xml,xslt,web-config,Xml,Xslt,Web Config,我需要使用xslt修改web.config。我们无法直接访问转换前或转换后的实际web.config。修改它的唯一机制是使用xslt 我已经编写了一个xslt,它将成功地添加其他节点。如果存在子节点,我可以添加子节点,或者创建父节点,必要时使用子节点完成 问题在于它必须是下的第一个节点,否则IIS会投诉。如果保证存在,这不会是个问题,但事实并非如此。无论我尝试了什么,我都无法强制结果可靠地将节点作为第一个节点 在我下面的示例中,如果不存在,它会将新的放在顶部,但如果它确实存在,它会将其添加到某个

我需要使用xslt修改web.config。我们无法直接访问转换前或转换后的实际web.config。修改它的唯一机制是使用xslt

我已经编写了一个xslt,它将成功地添加其他节点。如果存在子节点,我可以添加子节点,或者创建父节点,必要时使用子节点完成

问题在于
它必须是
下的第一个节点,否则IIS会投诉。如果保证存在,这不会是个问题,但事实并非如此。无论我尝试了什么,我都无法强制结果可靠地将
节点作为第一个节点

在我下面的示例中,如果
不存在,它会将新的
放在顶部,但如果它确实存在,它会将其添加到某个任意位置(在
标记内)

我到处都搜索过,但找不到这个看似简单的问题的解决方案,所以我希望一些XSLT专家能告诉我它真的很简单。应该注意的是,我在xslt(!)方面完全是个哑巴

示例源xml:

...
我的Xslt:
我不知道您在任意位置添加
configSections
是什么意思,如果它确实存在的话。XSLT将把它放在输出中的相同位置,除非
system.web
不存在,因为XSLT将把
system.web
放在任何现有
configSections
之前

无论如何,试试这个XSLT。这应该首先选择任何现有的
configSections
,如果不存在则添加一个。最后,它可以选择所有其他元素

<xsl:template match="configuration">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>  
        <xsl:apply-templates select="configSections"/>
        <xsl:if test="not(configSections)">
            <configSections>
                <section name="mySection"/>
           </configSections>
        </xsl:if>
        <xsl:if test="not(system.web)">
            <system.web>
                <httpRuntime executionTimeout="180" maxRequestLength="65536"/>
            </system.web>
        </xsl:if>
        <xsl:apply-templates select="node()[not(self::configSections)]"/>
    </xsl:copy>
</xsl:template>

太棒了!非常感谢你!通过“在任意位置添加”,它是在通过转换添加的新项之后,但在从源复制任何现有项(包括通过转换修改的现有项)之前添加它。不管怎样,你的答案很好。非常感谢。
<!-- Identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!--If <configSections> exists, then add in my <section> node-->
<xsl:template match="configSections">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <section name="mySectionHandler"/>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="configuration">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>      
        <xsl:if test="not(configSections)">
            <configSections>
                <section name="mySection"/>
           </configSections>
        </xsl:if>
        <xsl:if test="not(system.web)">
            <system.web>
                <httpRuntime executionTimeout="180" maxRequestLength="65536"/>
            </system.web>
        </xsl:if>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="configuration">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>  
        <xsl:apply-templates select="configSections"/>
        <xsl:if test="not(configSections)">
            <configSections>
                <section name="mySection"/>
           </configSections>
        </xsl:if>
        <xsl:if test="not(system.web)">
            <system.web>
                <httpRuntime executionTimeout="180" maxRequestLength="65536"/>
            </system.web>
        </xsl:if>
        <xsl:apply-templates select="node()[not(self::configSections)]"/>
    </xsl:copy>
</xsl:template>