xsl-list
[Top] [All Lists]

Re: Sorting network addresses

2005-03-02 06:44:44
Hmm...I must have gotten some stuff mixed up when pasting into the
email... Here is the XML, and XSLT exactly as I have it.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE networks [
<!ELEMENT networks (network+)>
<!ELEMENT network (address)>
<!ELEMENT address (#PCDATA)>
]>
<networks>
        <network>
                <address>1.1.1.1</address>
        </network>
        <network>
                <address>170.5.2.4</address>
        </network>
        <network>
                <address>2.3.1.2</address>
        </network>
</networks>


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
        <xsl:strip-space elements="*" />
        <xsl:variable name="newline">
                <xsl:text>
</xsl:text>
        </xsl:variable>

        <xsl:template match="/">
                <xsl:value-of select="$newline" />
                <networks>
                <xsl:value-of select="$newline" />
                                <xsl:for-each select="networks/network">
                                        <xsl:sort data-type="number" 
select="substring-before(address,'.')"/>
                                        <xsl:sort data-type="number"
select="substring-before(substring-after(address,'.'),'.')"/>
                                        <xsl:sort data-type="number"
select="substring-before(substring-after(substring-after(address,'.'),'.'),'.')"/>
                                        <xsl:sort data-type="number"
select="substring-after(substring-after(substring-after(address,'.'),'.'),'.')"/>
                                                <network>
                                                        <xsl:value-of 
select="$newline" />      
                                                        <address>
                                                                <xsl:value-of 
select="address" />
                                                        </address>
                                                        <xsl:value-of 
select="$newline" />
                                                </network>
                                                <xsl:value-of select="$newline" 
/>
                                </xsl:for-each>
                </networks>
        </xsl:template>
</xsl:stylesheet>


and here is the output

<?xml version="1.0" encoding="UTF-8"?>
                <networks>
                <network>
                <address>1.1.1.1</address>
                </network>
                <network>
                <address>1.1.1.1</address>
                </network>
                <network>
                <address>1.1.1.1</address>
                </network>
                <network>
                <address>1.1.1.1</address>
                </network>
                <network>
                <address>2.3.1.2</address>
                </network>
                <network>
                <address>170.5.2.4</address>
                </network>
                <network>
                <address>2.3.1.2</address>
                </network>
                <network>
                <address>2.3.1.2</address>
                </network>
                <network>
                <address>2.3.1.2</address>
                </network>
                <network>
                <address>170.5.2.4</address>
                </network>
                <network>
                <address>170.5.2.4</address>
                </network>
                <network>
                <address>170.5.2.4</address>
                </network>
                </networks>


I am using XMLSpy to generate the output.  To repeat how I did it,
open the XML document, go to the "XSL/XQuery" menu and choose "XSL
Transformation", point it to the XSLT and thats it.


....now do u get what i get?

thanks

On Wed, 2 Mar 2005 13:33:22 GMT, David Carlisle 
<davidc(_at_)nag(_dot_)co(_dot_)uk> wrote:


the result i am getting now is like this
Not from upur posted code, you can't: it generates srcaddr elements
there can't be any address elements in the output.

Running your posted stylesheet on your posted input I get:

$ saxon ip.xml ip.xsl
<?xml version="1.0" encoding="UTF-8"?>
                <networks>
                <network>
                <srcaddr>1.1.1.1</srcaddr>
                </network>
                <network>
                <srcaddr>2.3.1.2</srcaddr>
                </network>
                <network>
                <srcaddr>170.5.2.4</srcaddr>
                </network>
                </networks>

Note by the way your $newline is actually a newline and two tabs, hence
the strange indentaion in the result.

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




-- 

http://www.codecraig.com
http://jroller.com/page/codecraig

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--