nightclub
nightclub

Reputation: 721

Displaying XML nodes with XSL but keeping the order on XML - With multiple namespaces

I have the following multi namespaces XML file: objects.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="objects.xsl"?>
<objects
    xmlns:objA="http://www.mywebsite.com/objectA"
    xmlns:objB="http://www.mywebsite.com/objectB"
>
    <objA:objectA>
        <objA:attrA1>Instance 1 Value 1</objA:attrA1>
        <objA:attrA2>Instance 1 Value 2</objA:attrA2>
    </objA:objectA>
    <objB:objectB>
        <objB:attrB1>Instance 2 Value 1</objB:attrB1>
        <objB:attrB2>Instance 2 Value 2</objB:attrB2>
    </objB:objectB>
    <objA:objectA>
        <objA:attrA1>Instance 3 Value 1</objA:attrA1>
        <objA:attrA2>Instance 3 Value 2</objA:attrA2>
    </objA:objectA>
    <objB:objectB>
        <objB:attrB1>Instance 4 Value 1</objB:attrB1>
        <objB:attrB2>Instance 4 Value 2</objB:attrB2>
    </objB:objectB>
</objects>

and the following stylesheet XLST file: objects.xsl

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:objA="http://www.mywebsite.com/objectA"
    xmlns:objB="http://www.mywebsite.com/objectB"
>

<xsl:template match="/">
  <html>
  <body>

<xsl:for-each select="/objects/objA:objectA">
    (<xsl:value-of select="objA:attrA1"/> |
    <xsl:value-of select="objA:attrA2"/>)<br />
</xsl:for-each>

<xsl:for-each select="/objects/objB:objectB">
    (<xsl:value-of select="objB:attrB1"/> |
    <xsl:value-of select="objB:attrB2"/>)<br />
</xsl:for-each>

  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

But the result is not exactly what I want, because with above files I get:

(Instance 1 Value 1 | Instance 1 Value 2)
(Instance 3 Value 1 | Instance 3 Value 2)
(Instance 2 Value 1 | Instance 2 Value 2)
(Instance 4 Value 1 | Instance 4 Value 2)

which is not the same order as XML file. I need the same order as objects.xml: {1, 2, 3, 4}

I know the problem is the for-each statement applied on different points, but I give the above code to display an approximation of what I want.

I tried using only one for-each statement using a wildcard for namespace but it seems wildcards are not allowed for namespaces.

Any idea?

Upvotes: 0

Views: 23

Answers (1)

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

Try this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="/objects/*"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="*">
        <xsl:value-of select="concat('(', *[1],' | ', *[2],')')"/>
        <br/>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions