title: How to display XSD validated XML using XSLT
body: <p>I've been fighting with this for some time now, and haven't been able to find a clear answer to this yet.</p>
  
  <p>As I understand correctly I can store data in an XML file, validate it using an XSD and then display the data neatly using an XSLT.</p>
  
  <p>However I've been having issues trying to perform XPath queries to select the data I wish to display in my XSLT. When I use generic selectors like './/' or '*' I get the results I'd expect. However when I try to use more specific selectors like : 'root/responses' or any other variant hereof, I get no results.</p>
  
  <p>The XML file is validated correctly by the XSD, so I guess my data is at least somewhat correct. When I remove the XSD reference in the XML file, effectively removing the data validation, my XPath queries suddenly do work! Is there something I'm missing? I've tried adding namespace references to the XSLT but to no avail.</p>
  
  <p>I've described the XSD, Sample XL and Sample XSLT below. Any help or hints would be appreciated!</p>
  
  <hr>
  
  <p>The XSD, defining the structure, is as follows. This XSD describes a simple document, which nests three elements, and applies a restraint; the code of the responses'code must be unique.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;xs:schema id="uitext"
          targetNamespace="http://foo.bar/responsecode.xsd"
          elementFormDefault="qualified"
          xmlns:responsecodes="http://foo.bar/responsecode.xsd"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  
          &lt;xs:element name="root" type="responsecodes:rootType"&gt;
              &lt;xs:key name="responseCode"&gt;
                  &lt;xs:selector xpath="responsecodes:responses/responsecodes:response"&gt;
                      &lt;xs:annotation&gt;
                          &lt;xs:documentation&gt;All defined responsecodes&lt;/xs:documentation&gt;
                      &lt;/xs:annotation&gt;
                  &lt;/xs:selector&gt;
                  &lt;xs:field xpath="@code"&gt;
                      &lt;xs:annotation&gt;
                          &lt;xs:documentation&gt;Unique responsecode&lt;/xs:documentation&gt;
                      &lt;/xs:annotation&gt;
                  &lt;/xs:field&gt;
              &lt;/xs:key&gt;
          &lt;/xs:element&gt;
  
          &lt;xs:complexType name="rootType"&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="responses" minOccurs="1" maxOccurs="1" type="responsecodes:responseList"&gt;
                      &lt;xs:annotation&gt;
                          &lt;xs:documentation&gt;Defined responsecodes&lt;/xs:documentation&gt;
                      &lt;/xs:annotation&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
  
          &lt;xs:complexType name="responseList"&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="response" minOccurs="0" maxOccurs="unbounded" type="responsecodes:response"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
  
          &lt;xs:complexType name="response"&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"&gt;
                      &lt;xs:annotation&gt;
                          &lt;xs:documentation&gt;
                              Explains the use of the responsecode.
                          &lt;/xs:documentation&gt;
                      &lt;/xs:annotation&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
              &lt;xs:attribute name="code" type="xs:string" use="required"&gt;
                  &lt;xs:annotation&gt;
                      &lt;xs:documentation&gt;Unique code representing the response provided.&lt;/xs:documentation&gt;
                  &lt;/xs:annotation&gt;
              &lt;/xs:attribute&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:schema&gt;
  </code></pre>
  
  <p>An example XML document can be as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;?xml-stylesheet type="text/xsl" href="responsecode.xsl"?&gt;
  &lt;root xmlns="http://foo.bar/responsecode.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://foo.bar/responsecode.xsd responsecode.xsd"&gt;
      &lt;responses&gt;
          &lt;response code="firstCode"&gt;
              &lt;description&gt;Explanation of first code&lt;/description&gt;
          &lt;/response&gt;
          &lt;response code="secondCode"&gt;
              &lt;description&gt;Explanation of second code&lt;/description&gt;
          &lt;/response&gt;
          &lt;response code="thirdCode"&gt;
              &lt;description&gt;Explanation of third code.&lt;/description&gt;
          &lt;/response&gt;
      &lt;/responses&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>The test XSLT document referred to in the XML file is as follows. (This would display the codes as mentioned in a format that would resemble VS2008 enumeration definitions, but that aside)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:template match="/"&gt;
          &lt;html&gt;&lt;body&gt;&lt;h2&gt;Responses&lt;/h2&gt;
  
                  &lt;xsl:for-each select="root/responses/response"&gt;
                      &lt;xsl:choose&gt;
                          &lt;xsl:when test="description != ''"&gt;
                              &lt;br/&gt;'''&amp;lt;description&amp;gt;
                              &lt;br/&gt;'''&lt;xsl:value-of select="description" /&gt;
                              &lt;br/&gt;'''&amp;lt;/description&amp;gt;
                          &lt;/xsl:when&gt;
                      &lt;/xsl:choose&gt;
                      &lt;br/&gt;
                      &lt;xsl:value-of select="@code" /&gt;
  
                  &lt;/xsl:for-each&gt;
              &lt;/body&gt;
          &lt;/html&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>