简体   繁体   中英

How to replace this XML node's text with a passed in value using XSLT

This seems really simple but I've wasted a whole day trying to work this out:

Here is some simple 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>

All I want to do is replace the value in the <Text /> node with a value I'm passing in.

However I can't even get the templates to find the node and replace it with a hard coded value.

<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>

Can anyone see what's wrong please? All I get at the moment is a copy of the output (except that namespace declarations are on the same line)

In your xsl, replace xmlns="http://schemas.microsoft.com/analysisservices/2003/engine" to xmlns:text="http://schemas.microsoft.com/analysisservices/2003/engine" and add exclude-result-prefixes="text"

Then replace the template with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM