1

If I remove the Schema definition from my XML, then my XSLT works, but I can't get it to work with the Schema defined. I know this question has already been answered here, but I can't seem to get mine to work. I have the following XML header:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="student.xsl"?>
<Students xmlns="http:/www.example.com" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.example.com student.xsd">

<Student>
    <SSN>622-12-5748</SSN>
    <Name>
        <First-Name>Alexander</First-Name>
        <Last-Name>Mart</Last-Name>
    </Name>
    <Age>26</Age>
    <Institution>UCSF</Institution>
    <Email>Alexander@yahoo.com</Email>
</Student>
</Students>

Here is my XSLT file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fo="http://www.w3.org/1999/XSL/Format" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:fn="http://www.w3.org/2005/xpath-functions" 
xmlns:xsi="http://www.example.com">
<xsl:template match="/">
    <html>
        <body>
            <h2>Student Information</h2>
            <table border="1">
                <tr bgcolor="yellow">
                    <th>SSN</th>
                    <th>First Name</th>
                </tr>
                <xsl:for-each select="xsi:Students/Student">
                    <tr>
                        <td>
                            <xsl:value-of select="xsi:SSN"/>
                        </td>
                        <td>
                            <xsl:value-of select="xsi:Name/First-Name"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

What silly mistake am I making? Thanks in advance!

Community
  • 1
  • 1
Steph
  • 2,135
  • 6
  • 31
  • 44
  • Good question, +1. See my answer for explanation and solution. – Dimitre Novatchev Apr 30 '11 at 17:49
  • 1
    The most obvious "silly mistake" is the discrepancy between `http://www.example.com` in the stylesheet and `http:/www.example.com` in the source document. Added to this, using the prefix "xsi" for a namespace other than `http://www.w3.org/2001/XMLSchema-instance` is perfectly legal, but very likely to confuse your readers. – Michael Kay Apr 30 '11 at 21:50
  • Oh, well that discrepancy between the example.com references was just a mistake I made when translating my question to StackOverflow. That was not the problem in my actual document. Dimitre figured it out for me though. Thanks for the tip about not using xsi for the namespace. In my many attempts to fix this issue I tried that at one point because it was the prefix for my XMLSchema-instance in my schema document. I'm glad to know this is an inappropriate use of it though. – Steph May 01 '11 at 00:18
  • possible duplicate of [XSLT with XML source that has a default namespace set to xmlns](http://stackoverflow.com/questions/1344158/xslt-with-xml-source-that-has-a-default-namespace-set-to-xmlns) –  May 01 '11 at 02:36

2 Answers2

3

What silly mistake am I making? Thanks in advance!

Because the XML document is in a default namespace, and because the XML document has no node belonging to "no namespace", any unprefixed element names in any XPath expressions will not select anything.

You have started using prefixed names, but not completely.

Change:

           <xsl:for-each select="xsi:Students/Student"> 

to

           <xsl:for-each select="xsi:Students/xsi:Student"> 

and change:

<xsl:value-of select="xsi:Name/First-Name"/>

to

<xsl:value-of select="xsi:Name/xsi:First-Name"/>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
1

Try using "xsi:Students/xsi:Student" in your for-each select

Edit: And in your first name select as well: "xsi:Name/xsi:First-Name"

Hanz
  • 138
  • 3
  • 8