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 January 30th, 2007, 09:38 AM
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multi Node Condition

Hi,

I have the following problem:
I have an XML that describes a graph, some nodes describe nodes and their relations.
<Root>
   <Nodes>
      <Node Type="Main" Id="1000" Root="Y"></Node>
      <Node Type="Node" Id="1049">Bla</Node>
      <Node Type="Node" Id="1050">Bla1</Node>
      <Node Type="Node" Id="1051">Bla2</Node>
   </Nodes>
   <Relations>
      <Relation Type="Relation" From="1050" To="1049"></Relation>
      <Relation Type="Relation" From="1049" To="1051"></Relation>
   </Relations>
</Root>

I'm trying to construct a condition to find the Node Ids that are both From and To (the nodes that 'sits' in the middle).

I tried to create nested loops where the first loop finds the From and the inner loop tries to match the To but I don't know how to stop those loops (xsl:for-each) once the node is found.

I also need to use the id of the To/From node I have acquired using variable.

Any idea? thanks in advance,

Dror


 
Old January 30th, 2007, 09:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT is a declarative language so it doesn't have loops. The xsl:for-each instruction, which you might think of as a loop, is actually a mapping expression. That means it doesn't make sense to think of breaking out of it once some condition is found.

If you want to find all the nodes that appear in both an @From and an @To attribute, that's

/Root/Relations/@To[. = /Root/Relations/@From]



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 4th, 2007, 09:10 AM
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhKay,

Thanks for your replay. I've been trying to go on with this XSLT but I keep bumping into walls.
I tried to construct some more relations based on the above XML.
The first part of the desired result should look like this:
<Root>
    <Nodes>
        <Node Type="Main" Id="1000" Root="Y"></Node>
        <Node Type="Main" Id="1001" Root="Y" />
        <Node Type="Main" Id="1002" Root="Y" />
        <Node Type="Main" Id="1003" Root="Y" />
                <Node Type="Entity" Id="1050">Entity Desc</Node>
                <Node Type="Entity" Id="1049">Entity Desc</Node>
                <Node Type="Entity" Id="1051">Entity Desc</Node>
        </Nodes>

What I need is to go over all the 'Node' nodes and look them up within the relations.
I then need add some new 'Relation' nodes under 'Relations' to connect the entity nodes to the new Root nodes I've added.
The second part of my XML should look like this:
<Relations>
      <Relation Type="Relation" From="1050" To="1049"></Relation>
      <Relation Type="Relation" From="1049" To="1051"></Relation>
      <Relation Type="Basic" From="1001" To="1050"></Relation>
      <Relation Type="Basic" From="1002" To="1049"></Relation>
      <Relation Type="Basic" From="1003" To="1051"></Relation>
   </Relations>
</Root>

I tried to use <xsl:call-template> in order to create the new relations but my main problem is constructing the correct condition that will eventually call the named template that will create the new relations.

Thanks in advnace

Dror

 
Old February 4th, 2007, 11:33 AM
Registered User
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again,

One last question, I resolved almost everything but this.
How do I create/add a new 'Relation' node under 'Relations' when I'm going over the 'Node' elements?

Thanks in advanace,

Dror

 
Old February 4th, 2007, 04:55 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>How do I create/add a new 'Relation' node under 'Relations' when I'm going over the 'Node' elements?

You don't. You must organize your processing according to the structure of the output. Think of the result tree as being written serially: you can only construct an output node when you are constructing the part of the result tree in which it appears. But that's not a problem, because you can reference any part of the input tree at any time using XPath expressions.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Missing Node (Node data) pashworth XSLT 3 January 29th, 2007 12:39 PM
multi-multi-multiple Insert with subquery??? jmjyiannis SQL Server 2000 1 May 5th, 2006 02:20 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM





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