【问题标题】:problem with match namespace in xsltxslt 中匹配命名空间的问题
【发布时间】:2011-06-10 16:00:38
【问题描述】:

匹配元素有特定节点的问题。

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>

为什么还会输出“个人资料描述”?它有“基础”命名空间。

提前致谢。

【问题讨论】:

    标签: xslt namespaces


    【解决方案1】:

    简单的答案是:因为您从不告诉 XSLT 处理器忽略它。

    您提供了处理prf:* 的模板,但您不禁止处理base:。在没有其他条件的情况下,XSLT 处理器将默认行为built-in rules,也称为here)应用于它遇到的任何自定义模板未处理的任何节点。

    元素节点的默认行为是:

    • 将它们的文本节点子节点复制到输出,
    • 处理其子元素

    知道,您的 &lt;base:Description&gt;&lt;base:Text&gt; 元素会产生您所看到的内容。为了防止它,要么用空模板捕获它们:

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

    或通过为根节点定义模板来手动引导程序流程:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多