om471987 om471987 - 2 months ago 5
C# Question

XSLT transform doesn't recognize attribute of parent node

.Net XSLT parser doesn't recognize parent attribute (type) if there is child to that node (contributor). returns empty for following scenario but returns correct result if I remove child node.

Input XML

<role type="Actor">
<contributor />


<xsl:stylesheet xmlns:xsl="" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:value-of select="contributors/roles/role/@type" />



My C# method

public static XDocument TransformXML(string inputXMLString, string xslt)
var xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(inputXMLString));
inputXMLString = xmlDocumentWithoutNs.ToString();

var xslCompiledTransform = new XslCompiledTransform();
using (var stringReader = new StringReader(xslt))
using (var xmlReader = XmlReader.Create(stringReader))
using (var stringReader = new StringReader(inputXMLString))
using (var xmlReader = XmlReader.Create(stringReader))
using (var stringWriter = new StringWriter())
xslCompiledTransform.Transform(xmlReader, new XsltArgumentList(), stringWriter);
var resultXML = stringWriter.ToString();
var otuput = XDocument.Parse(resultXML);
return otuput;


Try without removing the NS. Its working for me :-)

var xmlDocumentWithoutNs = XElement.Parse(inputXMLString);