2013-08-06 32 views
1

我有下面一段XML:不能XSLT模板應用於XML元素

<?xml version="1.0"?> 
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml" id="new" cnxml-version="0.7" module-id="new"> 

<metadata xmlns:md="http://cnx.rice.edu/mdml" 
     mdml-version="0.5"> 

    <md:abstract>By the end of this section, you will be able to: 
     <list> 
      <item>Discuss the role of homeostasis in healthy functioning</item> 
      <item>Contrast negative and positive feedback, giving one physiologic example of each mechanism</item> 
     </list> 
    </md:abstract> 

,我需要應用模板MD:抽象元素。我有以下XSL代碼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:md="http://cnx.rice.edu/mdml/0.4"> 

<xsl:output omit-xml-declaration="yes" encoding="ASCII"/> 

<xsl:template match="c:document"> 
<body> 
    <xsl:apply-templates select="c:metadata"/> 
    <xsl:apply-templates select="c:content"/> 
</body> 
</xsl:template> 

<xsl:template match="c:metadata"> 
    <xsl:apply-templates select="md:abstract"/> 
</xsl:template> 

<xsl:template match="md:abstract"> 
    <xsl:apply-templates select="@*|node()"/> 
</xsl:template> 

但不幸的是,它並沒有捕捉MD:抽象元素。我無法弄清楚,想要是錯的。

回答

2

在您的文檔中您有xmlns:md="http://cnx.rice.edu/mdml",而在XSLT中您有xmlns:md="http://cnx.rice.edu/mdml/0.4"。這些不一樣。命名空間必須在詞法上相同才能匹配(沒有別名或超類)。

我自己經歷過這個,試圖管理版本化的命名空間,這是一場噩夢。要麼它在你的例子中失敗,要麼你最終明確檢查所有版本。如果它是您的名稱空間,請刪除該版本。

如果您正在設計它,您可以始終將版本置於單獨的屬性中(如XSLT轉換所做的那樣)。

+0

非常感謝,我真的錯過了。 –