我需要复制一个元素并更改这些子元素的值,这些子元素的名称以变量的值结尾,而其他子元素包含特定的日期值。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet>
<xsl:variable name="Date" select="<!--Contains '2019-04-01'"></xsl:variable>
<xsl:variable name="CurValue" select="<!--Contains '5.4321'-->"></xsl:variable>
<xsl:variable name="CurrCode" select="<!--Contains string 'USD' or 'EUR'-->"></xsl:variable>
<xsl:variable name="CurFieldName" select="concat( 'U_SFT_' , $CurrCode )"></xsl:variable>
<xsl:template match="/">
<vpf:Msg>
<xsl:call-template name="transform"></xsl:call-template>
</vpf:Msg>
</xsl:template>
<xsl:template name="transform">
<!--My transform-->
</xsl:template>
</xsl:stylesheet>这是输入xml:
<BigXml>
...
<Polish_FX_Vat_Window xmlns="">
<Code>2019</Code>
<Name nil="true"/>
<Canceled>N</Canceled>
<Object>SFT_oPolishFX</Object>
<LogInst nil="true"/>
<SFT_POLISHFXVATRCollection>
<SFT_POLISHFXVATR>
<Code>2019</Code>
<LineId>25</LineId>
<Object>SFT_oPolishFX</Object>
<LogInst nil="true"/>
<U_SFT_USD>3.745800</U_SFT_USD><!--I need update this field with value $CurVlaue. $CurrCode='USD'-->
<U_SFT_EUR>0.000000</U_SFT_EUR>
<U_SFT_Date>2019-04-01</U_SFT_Date><!--Date equals $Date variable-->
</SFT_POLISHFXVATR>
<SFT_POLISHFXVATR>
<Code>2019</Code>
<LineId>26</LineId>
<Object>SFT_oPolishFX</Object>
<LogInst nil="true"/>
<U_SFT_USD>13.000000</U_SFT_USD>
<U_SFT_EUR>0.000000</U_SFT_EUR>
<U_SFT_Date>2019-08-07</U_SFT_Date>
</SFT_POLISHFXVATR>
</SFT_POLISHFXVATRCollection>
</Polish_FX_Vat_Window>
</BigXml>我尝试在$CurFieldName语句中使用template match变量,但这没有起作用。
<Polish_FX_Vat_Window xmlns="">
<Code>2019</Code>
<Name nil="true"/>
<Canceled>N</Canceled>
<Object>SFT_oPolishFX</Object>
<LogInst nil="true"/>
<SFT_POLISHFXVATRCollection>
<SFT_POLISHFXVATR>
<Code>2019</Code>
<LineId>25</LineId>
<Object>SFT_oPolishFX</Object>
<LogInst nil="true"/>
<U_SFT_USD>5.4321</U_SFT_USD><!--New value-->
<U_SFT_EUR>0.000000</U_SFT_EUR>
<U_SFT_Date>2019-04-01</U_SFT_Date><!--Date equals $Date variable-->
</SFT_POLISHFXVATR>
<SFT_POLISHFXVATR>
<Code>2019</Code>
<LineId>26</LineId>
<Object>SFT_oPolishFX</Object>
<LogInst nil="true"/>
<U_SFT_USD>13.000000</U_SFT_USD><!--Leave value, Date not equals-->
<U_SFT_EUR>0.000000</U_SFT_EUR>
<U_SFT_Date>2019-08-07</U_SFT_Date>
</SFT_POLISHFXVATR>
</SFT_POLISHFXVATRCollection>
</Polish_FX_Vat_Window>$CurrCode变量甚至不能存储'EUR‘或’美元‘值。该值是在输入中动态确定的。
发布于 2019-08-01 13:22:28
您可以使用
<xsl:variable name="CurFieldName" select="concat('U_SFT_', $CurrCode )"></xsl:variable>若要更改此值,请在模板中匹配该值(与复制XML其余部分的标识模板相结合)。
...
<xsl:variable name="Date" select="'2019-04-01'"></xsl:variable>
<xsl:variable name="CurValue" select="'5.4321'"></xsl:variable>
<xsl:variable name="CurrCode" select="'USD'"></xsl:variable> <!-- Or 'USD' - can be set dynamically -->
<xsl:variable name="CurFieldName" select="concat('U_SFT_', $CurrCode )"></xsl:variable>在XSLT2.0中,可以在模板匹配规则中使用变量:
<xsl:template match="SFT_POLISHFXVATR/*[name()=$CurFieldName and ../U_SFT_Date=$Date]">
<xsl:copy>
<xsl:value-of select="$CurValue" />
</xsl:copy>
</xsl:template>在XSLT1.0中,它稍微复杂一些,模板包含一个all子元素-规则:
<xsl:template match="SFT_POLISHFXVATR/*">
<xsl:copy>
<xsl:choose>
<xsl:when test="name()=$CurFieldName and ../U_SFT_Date=$Date">
<xsl:value-of select="$CurValue" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()|@*" />
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>在这两种情况下,输出的相关部分如下所示
...
<SFT_POLISHFXVATR>
<Code>2019</Code>
<LineId>25</LineId>
<Object>SFT_oPolishFX</Object>
<LogInst nil="true"/>
<U_SFT_USD>5.4321</U_SFT_USD>
<!--I need update this field with value $CurVlaue. $CurrCode='USD'-->
<U_SFT_EUR>0.000000</U_SFT_EUR>
<U_SFT_Date>2019-04-01</U_SFT_Date>
<!--Date equals $Date variable-->
</SFT_POLISHFXVATR>
...https://stackoverflow.com/questions/57309577
复制相似问题