首先,了解Identity Transform
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
使用这意味着您只需为要转换的节点/属性编写模板。因此,在您的情况下,您需要一个模板来转换 AvgPx 属性
<xsl:template match="AllocInstrctn/@AvgPx">
<xsl:attribute name="AvgPx">
<xsl:value-of select="format-number(number() * number(../Instrmt/@Fctr), '0.00')" />
</xsl:attribute>
</xsl:template>
您还需要一个(更简单的)模板来转换Fctr 属性
<xsl:template match="Instrmt/@Fctr">
<xsl:attribute name="AvgPx">
<xsl:text>1</xsl:text>
</xsl:attribute>
</xsl:template>
就是这样!试试这个 XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="AllocInstrctn/@AvgPx">
<xsl:attribute name="AvgPx">
<xsl:value-of select="format-number(number() * number(../Instrmt/@Fctr), '0.00')" />
</xsl:attribute>
</xsl:template>
<xsl:template match="Instrmt/@Fctr">
<xsl:attribute name="AvgPx">
<xsl:text>1</xsl:text>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
应用于此 XML 时...
<FIXML>
<AllocInstrctn ID="108395820" TransTyp="1" AvgPx="0.35103" AvgParPx="0.35103">
<OrdAlloc ClOrdID="MANUAL" ClOrdID2="2634598" />
<Instrmt Fctr="1000" IssuCtry="ZA" />
</AllocInstrctn>
</FIXML>
以下是输出
<FIXML>
<AllocInstrctn ID="108395820" TransTyp="1" AvgPx="351.03" AvgParPx="0.35103">
<OrdAlloc ClOrdID="MANUAL" ClOrdID2="2634598"/>
<Instrmt AvgPx="1" IssuCtry="ZA"/>
</AllocInstrctn>
</FIXML>
编辑:如果您的实际 XML 具有名称空间,请尝试使用此 XSLT。注意开头的命名空间声明,以及使用前缀(在本例中为f:)来匹配命名空间中的元素。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:f="fixprotocol.org/FIXML-4-4">
<xsl:output method="xml" indent="yes" />
<xsl:template match="f:AllocInstrctn/@AvgPx">
<xsl:attribute name="AvgPx">
<xsl:value-of select="format-number(number() * number(../f:Instrmt/@Fctr), '0.00')" />
</xsl:attribute>
</xsl:template>
<xsl:template match="f:Instrmt/@Fctr">
<xsl:attribute name="AvgPx">
<xsl:text>1</xsl:text>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>