2012-05-22 90 views
2

我在使用网络设备中的XSLT转换某些XML数据时遇到了问题。考虑以下xml输出...使用XSLT对名称空间的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">元素。

是否有一些特殊的语法我应该使用?

+1

问题在哪里?你能否扩展你的问题来描述出了什么问题? – topskip

回答

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-itemenvironment-information可选择由刚刚命名该项目上(当然,照顾命名空间,当然)。

+0

非常感谢您的帮助,它的工作。 – user1409404

+1

@ user1409404我想你知道通过点击答案左侧的箭头来“接受”(也是upvoting)一个答案,你会发现这个解决方案对你有用(它让我很开心)。 – topskip

+0

有没有一种方法可以忽略名称空间“xmlns:junos = http://xml.juniper.net/junos/11.4X27/junos”,并遍历XSLT中的每个“environment-item”元素。 我想这样做的原因是,因为命名空间变化很频繁,每次都必须改变规则,这是一种皇家的痛苦。 – user1409404

相关问题