Hi,
I've got an XML file that I need to make changes to before creating an output XML file.
I've got an XSLT Transform file which does part of the job fine, but I'm needing to go one step further which is where I'm struggling.
In C# I'm using an XslCompiledTransform object to trigger the transform task and this works fine for the part that works so far, but I'm stuck for where to go from here.
I need to get an attribute value from the XML back into C# which will be put through a separate C++ DLL to get another value which will be inserted into the XML as additional attributes in the same node. It is likely that they'll want to add further values in using the same technique in the future, so it needs to be adaptable.
From what I've read so far, I think I can use the xsl:message element to bounce something back, and I should be able to pick this up in C# as an event fired by the XslCompiledTransform object. But, how do I get the information back from C# into the XslCompiledTransform object in order to add it to the output XML as attributes in the right place.
Some of the files I'll be processing through can be in excess of 20k records, and the XML structure is pretty in-depth. I'm not too bothered about how quick it is as it'll be badged as a data conversion task (following updates to the application that uses the original files). I'm happy to trade off some speed to have code that is easier to maintain in the future.
Example Input XML:
<RootNode>
<Person ID="1" Name="Bob Smith" Dept="Risks">
<Address Number="12" PostCode="A12 3BC" Country="UK"/>
</Person>
</RootNode>
Example Transform XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="RootNode">
<RootNode>
<xsl:foreach select="Person">
<Person>
<xsl:copy-of select="@ID"/>
<xsl:copy-of select="@Name"/>
<xsl:copy-of select="@Dept"/>
<xsl:foreach select="Address">
<xsl:copy-of select="@Number"/>
<!-- Need to take this PostCode value and pass back to C# -->
<xsl:copy-of select="@PostCode"/>
<!-- Based on what C# gets from the DLL, add the following attribute ("StreetName") if it doesn't already exist -->
<xsl:choose>
<xsl:when test="StreetName">
<xsl:copy-of select="@StreetName"/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="StreetName">
<xsl:value-of select="Test Road"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:copy-of select="@Country"/>
</xsl:foreach>
</Person>
</xsl:foreach>
</RootNode>
</xsl:template>
</xsl:stylesheet>
Expected output XML:
<RootNode>
<Person ID="1" Name="Bob Smith" Dept="Risks">
<Address Number="12" PostCode="A12 3BC" StreetName="Test Road" Country="UK"/>
</Person>
</RootNode>
In the above examples, I'm hoping to pass the PostCode back to C#, get the StreetName from a C++ DLL (by looking up the PostCode) and then pass the value back into the XSLT for adding to the XML.
Can anyone help?
Thanks in advance,
Jon