2011-04-03 23 views
0

我开始使用XSLT,我不明白这行:file:file在这个XSLT中代表什么?

<xsl:apply-templates select="file:file/file:description" /> 

为什么有3个file S' 我知道我的XML文件中的一个标签名为“file”,但为什么在前缀“file:”?

下面是实际的代码:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:file="http://phpdox.de/xml#" 
    exclude-result-prefixes="#default file" 
    > 
    <xsl:output method="html" indent="yes" encoding="utf-8" /> 

<xsl:template match="/"> 
    <html> 
    <body> 
     <xsl:apply-templates select="file:file/file:description" /> 
    </body> 
    </html> 
<xsl:template match="file:description"> 
    <header> 
     <p><xsl:value-of select="@compact" /></p> 
     <p><xsl:value-of select="file:description" /></p> 
    </header> 

</xsl:template> 
</xsl:stylesheet> 

和源XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<file xmlns="http://phpdox.de/xml#"> 
    <class> 
    <docblock> 
     <description compact="foo bar"/> 
    [...] 
+1

我怀疑源文件是否看起来像这样,因为它的内容与给定的命名空间'http:// phpdox.de/xml#'不匹配。 – Lucero 2011-04-03 22:47:51

+0

是的,你是对的,我已经清理了太多...我会编辑 – FMaz008 2011-04-03 23:10:32

回答

5

file:file/file:description三个file令牌中的两个是命名空间名称。在XML和XSLT中,a:b表示“在命名空间中的b”。你可以看到xmlns行解释说,file是一个你知道寻找它的名字空间。

所以字符串意味着找到file:description(这可能只是看起来像description如果一切文档中处于file命名空间)file:file下。合理?

+0

你知道任何方式来定义默认命名空间,因为每次添加“file:”前缀将是痛苦的,因为我现在只使用该命名空间... – FMaz008 2011-04-03 23:12:31

+0

要求作为一个单独的问题@ FMaz008 – 2011-04-03 23:13:33