I have created a little application to convert XML which I get out of our system, to a new XML format needed for customers using XSLT. The problem is, I can’t seem to be able to retrieve the values of the XML nodes, they’re just all empty.
Here is my XSLT file:
<?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="/">
<test>
<xsl:apply-templates select="SourceEndpoint" />
</test>
</xsl:template>
<xsl:template match="SourceEndpoint">
<InvoiceAmount>
<xsl:value-of select="." />
</InvoiceAmount>
</xsl:template>
</xsl:stylesheet>
My Original XML does have an node called SourceEndpoint so I’m not sure what I’m doing wrong here?
I also tried: <xsl:value-of select="Envelope/Header/SourceEndpoint" /> instead of a template but I got the same result
Edit
Here is a snippet my Original XML:
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
<Header>
<MessageId>{11EA62F5-543A-4483-B216-91E526AE2319}</MessageId>
<SourceEndpoint>Test</SourceEndpoint>
<DestinationEndpoint>TestInvoice</DestinationEndpoint>
</Header>
</Envelope>
Envelope is the root of the whole file
In the Transformed XML, test looks like: <test />
The XML file is in a default namespace — not in the “null” namespace. This makes a huge difference.
Search for XPath and default namespace and you’ll find numerous good answers.
In essence, you must define the same namespace in the XSLT transformation associating a prefix (say “x”) to it.
Then in any XPath expression (a match pattern is a kind of XPath expression) use
x:someNameinstead ofsomeName.One additional issue with the provided code is that it attempts to access the element
SourceEndpointas the top element of the document — and the top element in this case has a different name.Correcting these two issues, we get the following transformation:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced: