2011-04-03 22 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