繁体   English   中英

如何使用XSLT将XML节点的文本替换为传递的值

[英]How to replace this XML node's text with a passed in value using 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>

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

在您的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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