2011-06-10 123 views
0

匹配具有特定節點的元素是一個問題。xslt中匹配命名空間的問題

的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" herf="B1.xsl"?> 
<profile xmlns:base = "urn:mytest:baseInfo" 
xmlns:prf="http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#"> 
    <base:Description> 
     <base:text>description of profile</base:text> 
    </base:Description> 
    <prf:Component> 
     <prf:Keyboard>PhoneKeyPad</prf:Keyboard> 
     <prf:Model>SampleModel</prf:Model> 
     <prf:NumberOfSoftKeys>3</prf:NumberOfSoftKeys> 
     <prf:PixelAspectRatio>1x1</prf:PixelAspectRatio> 
     <prf:ScreenSize>128x240</prf:ScreenSize> 
    </prf:Component> 
</profile> 

和XSLT是:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:base = "urn:mytest:baseInfo" xmlns:prf="http://www.openmobilealliance.org/tech/profiles/UAPROF/ccppschema-20021212#"> 
<xsl:output method="xml" indent="yes"/> 


<xsl:template match="prf:*"> 
    <xsl:variable name="temp"> 
     <xsl:value-of select="local-name(.)"/> 
    </xsl:variable> 

    <xsl:element name="{$temp}"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 

結果是:

<?xml version="1.0" encoding="UTF-8"?>description of profile<Component> 
    <Keyboard>PhoneKeyPad</Keyboard> 
    <Model>SampleModel</Model> 
    <NumberOfSoftKeys>3</NumberOfSoftKeys> 
    <PixelAspectRatio>1x1</PixelAspectRatio> 
    <ScreenSize>128x240</ScreenSize> 
</Component> 

爲什麼 「配置文件的說明」 也輸出?它有「基礎」命名空間。

在此先感謝。

回答

3

簡單的答案是:因爲您從不告訴XSLT處理器忽略它。

您提供了一個處理prf:*的模板,但您不禁止處理base:。除此之外,XSLT處理器將默認行爲built-in rules,也here)應用於它遇到的任何自定義模板未處理的節點。

元素節點的默認行爲是:

  • 其文本節點孩子複製到輸出,
  • 過程中的子元素

知道了,你<base:Description><base:Text>元素產生正是你看到的。爲了防止它,無論是用空模板捕獲它們:

<xsl:template match="base:*" /> 

或通過定義根節點模板手動引導程序:

<xsl:template match="/"> 
    <xsl:apply-templates select="profile/prf:Component" /> 
</xsl:template>