使用XSLT遍历带有命名空间的XML元素

时间:2012-05-22 06:34:25

标签: xml xslt xpath

我在使用XSLT从网络设备转换某些XML数据时遇到问题。请考虑以下xml输出...

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.4X27/junos">
    <multi-routing-engine-results>
        <multi-routing-engine-item>
            <re-name>member0</re-name>
            <environment-information
                xmlns="http://xml.juniper.net/junos/11.4X27/junos-chassis">
                <environment-item>
                    <name>PEM 0</name>
                    <class>Temp</class>
                    <status>OK</status>
                    <temperature junos:celsius="30">30 degrees C / 86 degrees F
                    </temperature>
                </environment-item>
                <environment-item>
                    <name>PEM 1</name>
                    <class>Temp</class>
                    <status>OK</status>
                    <temperature junos:celsius="30">30 degrees C / 86 degrees F
                    </temperature>
                </environment-item>
            </environment-information>
        </multi-routing-engine-item>
        <multi-routing-engine-item>
            <re-name>member1</re-name>
            <environment-information
                xmlns="http://xml.juniper.net/junos/11.4X27/junos-chassis">
                <environment-item>
                    <name>PEM 0</name>
                    <class>Temp</class>
                    <status>OK</status>
                    <temperature junos:celsius="25">25 degrees C / 77 degrees F
                    </temperature>
                </environment-item>
                <environment-item>
                    <name>PEM 1</name>
                    <class>Temp</class>
                    <status>OK</status>
                    <temperature junos:celsius="25">25 degrees C / 77 degrees F
                    </temperature>
                </environment-item>
            </environment-information>
        </multi-routing-engine-item>
    </multi-routing-engine-results>
    <cli>
        <banner>{master:member0-re0}</banner>
    </cli>
</rpc-reply>

如何遍历XSLT中的每个“environment-item”元素。我现在有类似的东西......

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:for-each select="rpc-reply/multi-routing-engine-results/multi-routing-engine-item">
                ...
                <xsl:for-each select="./environment-information/environment-item">
                .....
                </xsl:for-each>
            </block>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet> 

但是当我的代码到达第二个循环<xsl:for-each select="./environment-information/environment-item">时,它不起作用。 我怀疑它与<environment-information xmlns="http://xml.juniper.net/junos/11.4X27/junos-chassis">元素有关。

我应该使用一些特殊的语法吗?

1 个答案:

答案 0 :(得分:2)

问题在于命名空间。 environment-informationenvironment-item位于“http://xml.juniper.net/junos/11.4X27/junos-chassis”命名空间中。这有效:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:a="http://xml.juniper.net/junos/11.4X27/junos-chassis">
  <xsl:template match="/" >
    <root>
    <xsl:for-each select="rpc-reply/multi-routing-engine-results/multi-routing-engine-item">
      <xsl:for-each select="a:environment-information/a:environment-item">
        <whatever></whatever>
      </xsl:for-each>
    </xsl:for-each>
    </root>
  </xsl:template>
</xsl:stylesheet> 

在内部循环前面不需要“./”,因为焦点位于元素multi-routing-engine-item上,只需命名该项目即可选择environment-information(好吧,照顾当然,名称空间。)