I am having a problem where nothing is being replaced.
I looked at other SO issues to rename nodes and to replace values, and have copied the examples from the answers, but nothing is working for me. I suspect that I have something subtly wrong, but I have reviewed my C#, XML, and XSL code, and can't seem to find it.
Here is my C# code:
private static string GetOutput(string xmlFileName, string xslFileName)
{
var xmlDocument = new XmlDocument();
xmlDocument.Load(xmlFileName);
var xslTransform = new XslCompiledTransform();
xslTransform.Load(xslFileName);
var stream = new MemoryStream();
xslTransform.Transform(xmlDocument, null, stream);
stream.Position = 1;
var reader = new StreamReader(stream);
string output = reader.ReadToEnd();
return output;
}
Here is my the contents of my XML file:
<?xml version="1.0" encoding="utf-8" ?>
<TransformThis xmlns="http://schemas.datacontract.org/2004/07/TransformTest"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<f_name>John</f_name>
<l_name>D'Oh!</l_name>
<Version>1.0</Version>
</TransformThis>
And here is the contents of my XSL 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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Version/text()[.='1.0']">1.1</xsl:template>
<xsl:template match="f_name">
<FirstName>
<xsl:apply-templates select="node()|@*"/>
</FirstName>
</xsl:template>
<xsl:template match="l_name">
<LastName>
<xsl:apply-templates select="node()|@*"/>
</LastName>
</xsl:template>
</xsl:stylesheet>
When I run my program, the output is the same as the original XML. Neither the f_name and l_name nodes have been renamed, nor has the Version node value been replaced.
Any help would be greatly appreciated.
UPDATE Per michael.hor257k's answer below, I have modified my XLS file as follows:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tx="http://schemas.datacontract.org/2004/07/TransformTest"
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="tx:Version/text()[.='1.0']">1.1</xsl:template>
<xsl:template match="tx:f_name">
<FirstName>
<xsl:apply-templates select="node()|@*"/>
</FirstName>
</xsl:template>
<xsl:template match="tx:l_name">
<LastName>
<xsl:apply-templates select="node()|@*"/>
</LastName>
</xsl:template>
</xsl:stylesheet>
I added the ...TransformTest namespace to the XLS file, and prefixed the match nodes with tx:.
Now the transformation is working, but it is also added the tx: namespace to the renamed nodes:
<?xml version="1.0" encoding="utf-8"?>
<Transformer xmlns="http://schemas.datacontract.org/2004/07/TransformTest"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<FirstName xmlns="" xmlns:tx="http://schemas.datacontract.org/2004/07/TransformTest">John</FirstName>
<LastName xmlns="" xmlns:tx="http://schemas.datacontract.org/2004/07/TransformTest">Doh</LastName>
<Version>1.1</Version>
</Transformer>
Also, when the XML is deserialized into an object, the LastName and FirstName properties are null, which I suspect has something to do with the additional namespace attributes on those nodes.
I would like the output to look just like the original XML, but with only the names and values changed.
UPDATE #2 michael.hor257k ADDED: an example XLS file in his answer. This resolved all of my issues.