[英]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.