om471987
om471987

Reputation: 5627

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

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

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <a>
      <xsl:value-of select="contributors/roles/role/@type" />
    </a>
  </xsl:template>
</xsl:stylesheet>

Output

<a></a>

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))
        {
            xslCompiledTransform.Load(xmlReader);
        }
        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;
        }
    }

Upvotes: 0

Views: 57

Answers (1)

Aishu
Aishu

Reputation: 413

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

var xmlDocumentWithoutNs = XElement.Parse(inputXMLString);

Upvotes: 1

Related Questions