2

我在使用来自网络设备的 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 中的每个“环境项”元素。我目前有类似以下内容....

<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">元素有关。

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

4

1 回答 1

2

问题在于名称空间。environment-information并且environment-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只需命名该项目即可选择(当然,请注意命名空间)。

于 2012-05-22T07:43:28.987 回答