2014-01-09 42 views
1

我有如下一個XML文檔的XML獲得元素的值:無法使用xsl

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="student.xsl"?> 
<StudentHeader> 
    <student> 
    <Roll>1</Roll> 
    <Name>KUMAR</Name> 
    <Sex>MALE</Sex> 
</student> 
</StudentHeader> 

',我使用下面的XSL表:

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> 
<xsl:template match="/"> 
    <xsl:apply-templates select="//Roll"/> 
    <xsl:apply-templates select="//Name"/> 
    <xsl:apply-templates select="//Sex"/> 
</xsl:template> 

<xsl:template match="Name"> 
<div style="color:purple"> 
<xsl:text> My Name is</xsl:text> 
     <xsl:value-of select="Name"/> 
</div> 
</xsl:template> 
</xsl:stylesheet> 

在輸出我得到只有文本格式綠色,像這樣:我的名字是

我的問題是:我沒有得到的名稱屬性的價值在xml.How我可以得到嗎?

回答

2

當前,您正在匹配其他<Name/>元素中的<Name/>元素。查詢當前上下文.text()節點,而不是:

<xsl:template match="Name"> 
<div style="color:purple"> 
<xsl:text> My Name is</xsl:text> 
     <xsl:value-of select="."/> 
</div> 
</xsl:template>