problem with match namespace in xslt
It's a problem with match elements which have specific node.
The 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>
and the XSLT is:
<?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:va开发者_运维知识库riable>
<xsl:element name="{$temp}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The result is:
<?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>
Why the "description of profile" is also output? It has "base" namespace.
Thanks in advance.
The simple answer is: Because you never tell the XSLT processor to ignore it.
You provide a template that processes prf:*
, but you do not forbid the processing of base:
. Given nothing else, the XSLT processor applies default behavior (built-in rules, also here) to any nodes it encounters that are not handled by any custom template.
The default behavior for element nodes is:
- copy their text-node children to the output,
- process their child elements
Knowing that, your <base:Description>
and <base:Text>
elements produce exactly what you see. To prevent it, either catch them with an empty template:
<xsl:template match="base:*" />
or direct the program flow manually by defining a template for the root node:
<xsl:template match="/">
<xsl:apply-templates select="profile/prf:Component" />
</xsl:template>
精彩评论