【问题标题】:Change inner node with a variable value使用变量值更改内部节点
【发布时间】:2021-01-05 03:40:32
【问题描述】:

我想复制一个 xml 并用一个变量替换内部文本。 这是我创建的 xslt

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns2="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" exclude-result-prefixes="ns2">
 <xsl:param name="InstrumentationKey" /> 
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="ns2:InstrumentationKey">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>  
        <xsl:text>
            <xsl:value-of select="$InstrumentationKey" />
        </xsl:text>
    </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

上面的xslt是为了转化下面的XML

<?xml version="1.0" encoding="utf-8"?><ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings"><InstrumentationKey>TestData</InstrumentationKey></ApplicationInsights>

我收到了错误,即 xsl:text 中不允许

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    找到答案。应该是

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns2="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" exclude-result-prefixes="ns2">
     <xsl:param name="InstrumentationKey" /> 
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="ns2:InstrumentationKey">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>  
            <xsl:value-of select="$InstrumentationKey" />
        </xsl:copy>
     </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 2018-12-25
      相关资源
      最近更新 更多