Reputation: 536
I have the following xml
<FRA>
<Id>USD Libor Futures</Id>
<PriceBasis>EuroDollar</PriceBasis>
</FRA>
I would like the following output
<FRA>
<Id>USD Libor Futures</Id>
<PriceBasis>EuroDollar</PriceBasis>
<ModifyMktData srcontractid="USD Libor Futures Convexity" optype="add" srctype="Spread" dsttype="Price"/>
</FRA>
I am using the following xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="FRA/PriceBasis">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:variable name="fraid" select="Id"/>
<ModifyMktData>
<Update srccontractid="{$fraid} Convexity" optype="add" srctype="Spread" dsttype="Price"/>
</ModifyMktData>
</xsl:template>
</xsl:stylesheet>
The output from the xslt looks like this:
<FRA>
<Id>USD LIBOR Futures</Id>
<PriceBasis>EuroDollar</PriceBasis>
<ModifyMktData>
<Update srccontractid=" Convexity" optype="add" srctype="Spread" dsttype="Price" />
</ModifyMktData
</FRA>
I am essentially looking to reuse the Id in the attribute for ModifyMktData. Obviously the xslt I have does not work as I would like it to. It does not add the Id field to the srccontractid field. Thanks in advance.
Upvotes: 0
Views: 41
Reputation: 22617
The problem with your code is context. At the point where you define a variable:
<xsl:variable name="fraid" select="Id"/>
You are inside a template that matches PriceBasis
elements. This means that the context of this variable definition is a particular PriceBasis
element. But, apparently, a PriceBasis
element does not have a child element Id
.
Instead, Id
is a child element of FRA
. In this context, the right path to Id
is:
../Id
The stylesheet below simplifies the code, it does not use a variable at all. For an expression as short as ../Id
I don't see why you would need a variable. You can place a path expression directly inside the attribute value template brackets:
{../Id}
Stylesheet
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PriceBasis">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<ModifyMktData>
<Update srccontractid="{../Id} Convexity" optype="add" srctype="Spread" dsttype="Price"/>
</ModifyMktData>
</xsl:template>
</xsl:stylesheet>
XML Input
Your input is not well-formed and will not be accepted by any XML application. Assuming the following input, where the Fra
element is closed again:
<FRA>
<Id>USD Libor Futures</Id>
<PriceBasis>EuroDollar</PriceBasis>
</FRA>
XML Output
<?xml version="1.0" encoding="utf-8"?>
<FRA>
<Id>USD Libor Futures</Id>
<PriceBasis>EuroDollar</PriceBasis>
<ModifyMktData>
<Update srccontractid="USD Libor Futures Convexity" optype="add" srctype="Spread" dsttype="Price"/>
</ModifyMktData>
</FRA>
Upvotes: 1
Reputation: 529
First, your template is matching FRA/PriceBasis
, which doesn't exist in the input. It looks like you want to match just FRA
instead.
Second, you're creating an Update
node under your ModifyMktData
node, which is not in your desired output as listed above.
Aside from that, what you're doing looks like it should work.
Upvotes: 0