1

I know I'm missing something simple here, yet I can't figure it out. I have other, more complex, XML and XSLT that are working but for some reason I can't get this specific one going. I believe it's the structure of the XML file that's being generated during serialization.

What I'm looking to do is get the value of an XML element and display it in HTML. I've taken everything else away except the specific areas related to this issue.

In the "html" variable in the code, the value for location is always blank.

XML

<WidgetBuilder>
  <DefaultLocation>1234</DefaultLocation>
</WidgetBuilder>

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes" />

  <xsl:template match="/">
    LOCATION: '<xsl:value-of select="DefaultLocation" />'
  </xsl:template>

</xsl:stylesheet>

Code

string xml = File.ReadAllText(@"..\..\InitXml1.xml");
string xslt = File.ReadAllText(@"..\..\InitXslt1.xslt");

XPathDocument doc = new XPathDocument(new StringReader(xml));
XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(XmlReader.Create(new StringReader(xslt)));

StringWriter sw = new StringWriter();
xslTransform.Transform(doc, null, sw);

string html = sw.ToString();
Console.WriteLine(html);

1 Answer 1

3

Your XSL template matches the document root node, not the document element (they're not the same thing). Try:

<xsl:value-of select="WidgetBuilder/DefaultLocation" />

EDIT: Also, since you're using a default namespace, you'll have to make it visible from your stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:dc="schemas.datacontract.org/2004/07/YourFullClassName"
                version="1.0" exclude-result-prefixes="msxsl">
    <xsl:output method="html" indent="yes" />

    <xsl:template match="/">
    LOCATION: '<xsl:value-of select="dc:WidgetBuilder/dc:DefaultLocation" />'
    </xsl:template>

</xsl:stylesheet>

See here for a detailed explanation and other use cases.

Sign up to request clarification or add additional context in comments.

5 Comments

That works in the sample I provided, thank you. I know I tried this in the full blown code, and I tried it again and it's not working there. So, something is different between the two which is blowing my mind because they're really not all that different. Just some HTML before that statement in the XSLT and more nodes inline (no more structure to it that what I'm showing) in the XML document.
@Chris, that's indeed strange. You're not using namespaces, are you?
In fact I am, there's an attribute of "xmlns" on the document root node. It's generated by C# when the class is serialized. I didn't include it as it has client specific information in it. How would that change the XSLT?
It looks like: "<WidgetBuilder xmlns="schemas.datacontract.org/2004/07/more-here" xmlns:i="w3.org/2001/XMLSchema-instance">"
@Chris, IIRC that would make us unable to match WidgetBuilder using its non-qualified name. Since it's a default namespace, we probably can't use a prefix. I'll dig around and get back to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.