Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2003, 08:28 AM
Registered User
 
Join Date: Jul 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select distinct elements

I have a configuration file with global and specific settings, i.e.:

<?xml version="1.0" encoding="iso-8859-1"?>
<conf>
    <global>
        <settings>
            <service>global service</service>
            <network>global network</network>
            <port>global port</port>
        </settings>
    </global>

    <instance id="1">
        <settings>
            <service>instance service 1</service>
            <daemon>instance deamon 1</daemon>
        </settings>
    </instance>

    <instance id="2">
        <settings>
            <service>instance service 2</service>
            <network>instance network 2</network>
            <daemon>instance deamon 2</daemon>
            <port>instance port 2<port>
        </settings>
    </instance>

    <instance id="3">
        <settings>
            <service>instance service 3</service>
            <network>instance network 3</network>
            <daemon>instance deamon 3</daemon>
        </settings>
    </instance>
</conf>

I want to transform this into a new XML document with settings taken from the global and from a specific instance. The specific instance have to override the global settings so i figured if I take all settings from the specified instance and then append settings from global that are not present in the specific. Below shown how the output would look like if I specified id=1:

<?xml version="1.0" encoding="iso-8859-1"?>
<conf>
    <settings>
        <service>instance service 1</service>
        <network>global network</network>
        <port>global port</port>
        <daemon>instance deamon 1</daemon>
    </settings>
</conf>

If I specified id=2:

<?xml version="1.0" encoding="iso-8859-1"?>
<conf>
    <settings>
        <service>instance service 2</service>
        <network>instance network 2</network>
        <daemon>instance deamon 2</daemon>
        <port>instance port 2<port>
    </settings>
</conf>

I thougt about using the DOM but I want to learn how I would do in XSLT. Could someone please advice me or give me a hint.

Tnx
/Andrin
 
Old August 2nd, 2003, 03:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi Andrin,

the problem interested me actually(there are many situations where the technique will be useful), so I tried to solve it entirely instead of giving a hint ;)

Here is the stylesheet:

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:param name="id"/>

    <xsl:output indent="yes" method="xml"/>

    <xsl:variable name="global-settings" select="/conf/global/settings/*"/>

    <xsl:template match="/">
        <conf>
            <xsl:apply-templates select="conf/instance[@id = $id]"/>
        </conf>
    </xsl:template>


    <xsl:template match="instance/settings">
        <xsl:variable name="children" select="*"/>
        <xsl:variable name="local-setting-names">
            <xsl:for-each select="$children">
                <xsl:element name="{name(.)}">
                    <xsl:value-of select="name(.)"/>
                </xsl:element>
            </xsl:for-each>
        </xsl:variable>

        <settings>
            <xsl:copy-of select="$children | $global-settings[not(name() = $local-setting-names/*)]"/>
        </settings>
    </xsl:template>
</xsl:stylesheet>

The stylesheet requires RTF-to-nodeset conversion mechanism: I used Saxon 6.5.2 and put the "version" attribute value equal to 1.1 (this is sufficient for Saxon to implicitly perform any such conversion if needed; for other XSLT processors you have to use vendor's node-set(RTF) function explicitly, if it's supported of course). However, it's interesting to see a stylesheet which will solve the probelm without using a temporary tree.

Regards,
Armen
 
Old August 4th, 2003, 04:07 AM
Registered User
 
Join Date: Jul 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Armen,

Again thanks :-) I also thought that this would be a thing that could be used in many situations. I also thougt that someone must have been in my position before and done this thing.

This example was a little bit easier to understand. At work we are using MSXML and by using the node-set() function as you said it worked. I personaly prefer to use Xalan or Saxon but what to do when ten other persons want to use Microsofts parser ;)

Thanks
/Andrin





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Distinct kirkmc Access 13 July 13th, 2006 01:26 PM
Select Distinct kirkmc Excel VBA 3 May 5th, 2006 07:55 PM
Select Distinct? hugh@kmcnetwork.com SQL Language 5 November 5th, 2005 09:58 AM
Distinct SELECT DISTINCT question... EndEffect Classic ASP Databases 4 August 18th, 2005 08:53 AM
select distinct bmains ADO.NET 0 April 8th, 2004 02:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.