2009-10-28 48 views
5

我有這個XML如何獲取XSLT中根元素的屬性值?

<?xml version="1.0" encoding="utf-8" ?> 
<IMPORT mode="FULL"> 
    .... 
</IMPORT> 

我想它有以下XSLT樣式轉換:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <xsl:element name="import"> 
     <xsl:attribute name="mode"> 
     <xsl:value-of select="@mode"/> 
     </xsl:attribute> 
      .... 
     </xsl:element> 
    </xsl:template> 

我的問題是,下面的行似乎並沒有工作:

<xsl:value-of select="@mode"/> 

正如我只得到

<import mode=""> 

,而不是預期:

<import mode="FULL"> 

任何線索?

回答

7

嘗試使用<xsl:value-of select="IMPORT/@mode"/>

+0

謝謝大家對你的答案 – user198063 2009-10-28 13:55:40

8

您的IMPORT元素是不實際。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <xsl:apply-templates select="IMPORT"/> 
    </xsl:template> 
    <xsl:template match="IMPORT"> 
    <xsl:element name="import"> 
     <xsl:attribute name="mode"> 
     <xsl:value-of select="@mode"/> 
     </xsl:attribute> 
     ....  
    </xsl:element> 
    </xsl:template> 
1

試試這個:

<xsl:template match="IMPORT"> 
    <xsl:element name="import"> 
     <xsl:attribute name="mode"> 
      <xsl:value-of select="@mode"/> 
      </xsl:attribute> 
    </xsl:element> 
</xsl:template>