XSLT does not work if my XML file has a Schema associated with it
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!
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"/>
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"
精彩评论