问题
Following is an extract of the XSL stylesheet that I have written. It says my '.xsl' is well formed but it somehow does not retrieve values from the XML, instead just pastes the header and table headings.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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">
<xsl:template match="/" >
<html>
<head>
<title>GlenMark Pharma</title>
</head>
<h1 align="center"><font face="Monotype Corsiva" color="red">GlenMarkPharma</font></h1>
<body>
<table>
<tbody>
<tr>
<th>ID</th>
<th>ENAME</th>
<th>Mobile</th>
<th>EMAIL</th>
<th>PWD</th>
</tr>
<xsl:for-each select="employees/employee">
<tr>
<td><xsl:value-of select="@empID" /></td>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="mobile" /></td>
<td><xsl:value-of select="email" /></td>
<td><xsl:value-of select="pwd" /></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here is my XML doc:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="MyfirstXsl.xsl"?>
<!DOCTYPE employees SYSTEM "Mydtd.dtd">
<me:employees xmlns:me="pavitar.dua@gmail.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="pavitar.dua@gmail.com Mycomplex.xsd">
<me:employee empID="EMP101">
<me:name>Vicky</me:name>
<me:mobile>9870582356</me:mobile>
<me:email>vicky@gmail.com</me:email>
<me:pwd>&defPWD;</me:pwd>
</me:employee>
</me:employees>
I have also tried writing:
<xsl:for-each select="employees/employee">
<tr>
<td><xsl:value-of select="@empID" /></td>
<td><xsl:value-of select="me:name" /></td>
In the XSL since I have an alias to the namespace in my XML Doc.But it gives me an error of Invalid Pre-fix.I don't know what I'm doing wrong.
回答1:
Add two namespace declarations into your XSLT:
xmlns:me="pavitar.dua@gmail.com"
to denote the input marktup;xmlns="http://www.w3.org/1999/xhtml"
to denote the resulting markup.
And then change your XSLT to denote the source with the me
prefix like this:
<xsl:for-each select="me:employees/me:employee">
<tr>
<td><xsl:value-of select="@empID" /></td>
<td><xsl:value-of select="me:name" /></td>
<td><xsl:value-of select="me:mobile" /></td>
<td><xsl:value-of select="me:email" /></td>
<td><xsl:value-of select="me:pwd" /></td>
</tr>
</xsl:for-each>
Also, consider using a “more XSLT-native” constructs:
<xsl:apply-templates/> <!-- instead of the for-each --!>
…
<xsl:template match="me:employee">
<!-- include the table row here --!>
</xsl:template>
You will end up with a more versatile XSLT design. Bear in mind that XSLT is supposed to be declarative not procedural.
回答2:
Your XSLT is unaware of the namespace used in your XML i.e. "pavitar.dua@gmail.com". Here is the modified XSL. Just added
xmlns:me="pavitar.dua@gmail.com"
To your XSLT and verified it is working
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:me="pavitar.dua@gmail.com" version="2.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">
<xsl:template match="/" >
<html>
<head>
<title>GlenMark Pharma</title>
</head>
<h1 align="center"><font face="Monotype Corsiva" color="red">GlenMarkPharma</font></h1>
<body>
<table>
<tbody>
<tr>
<th>ID</th>
<th>ENAME</th>
<th>Mobile</th>
<th>EMAIL</th>
<th>PWD</th>
</tr>
<xsl:for-each select="me:employees/me:employee">
<tr>
<td><xsl:value-of select="@empID" /></td>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="mobile" /></td>
<td><xsl:value-of select="email" /></td>
<td><xsl:value-of select="pwd" /></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/4533656/why-does-this-xsl-fails-to-retrieve-value