user1409404
user1409404

Reputation: 35

Iterate through XML element with namespace using XSLT

I’m having trouble transforming some XML data using XSLT from a network device. Consider the following xml output...

<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>

How do I iterate through each of the “environment-item” elements in XSLT. I have something like the following currently....

<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> 

However my code does not work when it reaches the second loop <xsl:for-each select="./environment-information/environment-item">. I suspect it is too do with the <environment-information xmlns="http://xml.juniper.net/junos/11.4X27/junos-chassis"> element.

Is there some special syntax I should be using?

Upvotes: 2

Views: 1250

Answers (1)

topskip
topskip

Reputation: 17375

The problem is with namespaces. environment-information and environment-item are in the "http://xml.juniper.net/junos/11.4X27/junos-chassis" namespace. This works:

<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> 

You don't need the "./" in front of the inner loop as the focus is on the element multi-routing-engine-item and environment-information is selectable by just naming that item (well, take care of the namespace, of course).

Upvotes: 2

Related Questions