tags:

views:

50

answers:

1

I've got XML like this:

<XML>
    <dealings>
        <members>
            <member path="Swap.1.&apos;stream#1.Schedule&apos;" source="0" type="string">CONTEND</member>
            <member path="Swap.1.&apos;stream#2.Schedule&apos;" source="0" type="string">CONTEND</member>
            <member path="Swap.1.&apos;stream#3.Schedule&apos;" source="0" type="string">CONTEND</member>
        </members>
    </dealings>
</XML>

And I need to do a transform to, besides change the content, get the number after the stream# to use in the name of a node, getting something like this:

<XML>
    <flows-1>
        CONTEND
    </flows-1>
    <flows-2>
        CONTEND
    </flows-2>
    <flows-3>
        CONTEND
    </flows-3>  
</XML>

I don't get the proper way to get the number. It is important to get the number, not list in order or by guessing, because the number may not be in order, sequential or even start with 1.

+4  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="/">
        <XML>
            <xsl:apply-templates/>
        </XML>
    </xsl:template>
    <xsl:template match="member">
        <xsl:element name="flows-{substring-before(substring-after(@path,'#'),'.')}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Result (proper) output:

<XML>
    <flows-1>CONTEND</flows-1>
    <flows-2>CONTEND</flows-2>
    <flows-3>CONTEND</flows-3>
</XML>
Alejandro
Good answer (+1).
Dimitre Novatchev