为什么此 XSL 无法检索值? [英] Why does this XSL fails to retrieve value?

查看:22
本文介绍了为什么此 XSL 无法检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我编写的XSL 样式表的摘录.它说我的 '.xsl' 格式正确,但它不从 XML 中检索值,而只是粘贴标题和表格标题.

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>

这是我的 XML 文档:

<?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>

我也尝试过写作:

<xsl:for-each select="employees/employee">
                        <tr>
                            <td><xsl:value-of select="@empID" /></td>
                            <td><xsl:value-of select="me:name" /></td>

在 XSL 中,因为我的 XML 文档中有一个名称空间的别名.但它给了我无效前缀的错误.我不知道我做错了什么.

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.

推荐答案

在 XSLT 中添加两个命名空间声明:

Add two namespace declarations into your XSLT:

  • xmlns:me="pavitar.dua@gmail.com" 表示输入标记;
  • xmlns="http://www.w3.org/1999/xhtml" 表示结果标记.
  • xmlns:me="pavitar.dua@gmail.com" to denote the input marktup;
  • xmlns="http://www.w3.org/1999/xhtml" to denote the resulting markup.

然后更改您的 XSLT 以使用 me 前缀表示源,如下所示:

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>

此外,请考虑使用更多 XSLT 原生"结构:

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>

您最终会得到更通用的 XSLT 设计.请记住,XSLT 应该是声明性而不是程序性的.

You will end up with a more versatile XSLT design. Bear in mind that XSLT is supposed to be declarative not procedural.

这篇关于为什么此 XSL 无法检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆