如何使用XSLT用传入的值替换此XML节点的文本

时间:2014-02-03 06:22:13

标签: xml xslt

这看起来很简单,但我浪费了一整天的努力来解决这个问题:

这是一些简单的xml

<Cube xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ddl2="http://schemas.microsoft.com/analysisservices/2003/engine/2" 
xmlns:ddl2_2="http://schemas.microsoft.com/analysisservices/2003/engine/2/2" 
xmlns:ddl100_100="http://schemas.microsoft.com/analysisservices/2008/engine/100/100" 
xmlns:ddl200="http://schemas.microsoft.com/analysisservices/2010/engine/200" 
xmlns:ddl200_200="http://schemas.microsoft.com/analysisservices/2010/engine/200/200" 
xmlns:ddl300="http://schemas.microsoft.com/analysisservices/2011/engine/300" 
xmlns:ddl300_300="http://schemas.microsoft.com/analysisservices/2011/engine/300/300" 
xmlns:ddl400="http://schemas.microsoft.com/analysisservices/2012/engine/400" 
xmlns:ddl400_400="http://schemas.microsoft.com/analysisservices/2012/engine/400/400" 
xmlns:dwd="http://schemas.microsoft.com/DataWarehouse/Designer/1.0" 
dwd:design-time-name="56e2650a-1176-4739-be4f-e82aaaf501dd" 
xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
  <MdxScripts>
    <MdxScript dwd:design-time-name="c1bfa7b6-d041-4a75-918e-28822b676582">
      <Commands>
        <Command>
            <Text>
                    OLD
            </Text>
        </Command>
      </Commands>
    </MdxScript>
  </MdxScripts>
</Cube>

我想做的就是用我传入的值替换<Text />节点中的值。

但是我甚至无法获取模板来查找节点并将其替换为硬编码值。

<xsl:stylesheet
  version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ddl2="http://schemas.microsoft.com/analysisservices/2003/engine/2"
    xmlns:ddl2_2="http://schemas.microsoft.com/analysisservices/2003/engine/2/2"
    xmlns:ddl100_100="http://schemas.microsoft.com/analysisservices/2008/engine/100/100"
    xmlns:ddl200="http://schemas.microsoft.com/analysisservices/2010/engine/200"
    xmlns:ddl200_200="http://schemas.microsoft.com/analysisservices/2010/engine/200/200"
    xmlns:ddl300="http://schemas.microsoft.com/analysisservices/2011/engine/300"
    xmlns:ddl300_300="http://schemas.microsoft.com/analysisservices/2011/engine/300/300"
    xmlns:ddl400="http://schemas.microsoft.com/analysisservices/2012/engine/400"
    xmlns:ddl400_400="http://schemas.microsoft.com/analysisservices/2012/engine/400/400"
    xmlns:dwd="http://schemas.microsoft.com/DataWarehouse/Designer/1.0"
    dwd:design-time-name="56e2650a-1176-4739-be4f-e82aaaf501dd"
    xmlns="http://schemas.microsoft.com/analysisservices/2003/engine"
>



    <xsl:param name="replacementScript">some passed in value</xsl:param>

    <xsl:output method="xml" encoding="utf-8" cdata-section-elements="value" indent="yes" />

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

    <xsl:template match="/Cube/MdxScripts/MdxScript/Commands/Command/Text">
            SOME HARD CODED VALUE
    </xsl:template>


</xsl:stylesheet>

有人能看到什么问题吗?我现在得到的只是输出的副本(除了命名空间声明在同一行上)

1 个答案:

答案 0 :(得分:1)

在您的xsl中,将xmlns="http://schemas.microsoft.com/analysisservices/2003/engine"替换为xmlns:text="http://schemas.microsoft.com/analysisservices/2003/engine"并添加exclude-result-prefixes="text"

然后用

替换模板
<xsl:template match="/text:Cube/text:MdxScripts/text:MdxScript/text:Commands/text:Command/text:Text/text()">
    <xsl:value-of select="$replacementScript"/>
</xsl:template>