2

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?

1
  • 1
    Where is the problem? Could you extend your question to describe what went wrong? Commented May 22, 2012 at 6:42

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

@user1409404 I guess you know that by "accepting" (and also upvoting) an answer by clicking on the arrow on the left side of my answer you show that this solution worked for you (and it makes me happy).
Is there a way I can ignore the namespace “xmlns:junos=xml.juniper.net/junos/11.4X27/junos” and iterate through each of the “environment-item” elements in XSLT. The reason I would like to do this is, because the namespace changes quite often and it is a royal pain having to change the rule everytime.
@user1409404 I think you're better off with a new question, so more people can see it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.