473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Select Node Using position or value of another node.

Hi,

I'm trying to get the value of another node using the position of
another node or the name of the tag.
Such that the current node is one of the contacts child nodes sec or
prim
and doing
<xsl:value-of select="//cust[seq = position()]/name"/>
Should return jon for the prim tag and peter for the sec tag, but
fails as position() appears to resolve to the position of cust not the
contacts node

alternatively I would like to do something like

<xsl:value-of select="//cust[seq = [name()='prim' then 0 else
1]]/name"/>

<custs>
<cust>
<seq>0</seq>
<name>jon</seq>
</cust>
<cust>
<seq>1</seq>
<name>peter</seq>
</cust>
<contacts>
<prim>
<message>hello</message>
</prim>
<sec>
<message>hello</message>
</sec>
</contacts>
</custs>

Oct 14 '05 #1
6 1864
Eddy C wrote:
Hi,

I'm trying to get the value of another node using the position of
another node or the name of the tag.
For the difference between tags and elements, see the XML FAQ at
http://xml.silmaril.ie/authors/makeup/
Such that the current node is one of the contacts child nodes sec or
prim
and doing
<xsl:value-of select="//cust[seq = position()]/name"/>
Should return jon for the prim tag and peter for the sec tag, but
fails as position() appears to resolve to the position of cust not the
contacts node
In cust[...position()...], the value of position() refers to the position of
the cust among its siblings, starting at 1.
alternatively I would like to do something like

<xsl:value-of select="//cust[seq = [name()='prim' then 0 else
1]]/name"/>
This is not a well-formed document. I assume you meant to use </name>
after jon and peter.

If so, cust[position()=seq+1]/name will return jon and peter.
<custs>
<cust>
<seq>0</seq>
<name>jon</seq>
</cust>
<cust>
<seq>1</seq>
<name>peter</seq>
</cust>
<contacts>
<prim>
<message>hello</message>
</prim>
<sec>
<message>hello</message>
</sec>
</contacts>
</custs>


I'm not clear what you want from this. Are you trying to access the
message belonging to each name, or the name belonging to each message?

///Peter

Oct 15 '05 #2
Sorry Peter, I'll try and be a little clearer. There was a mistake in
the XML, which you correclty identified.

The idea is to fetch the name from the cust node, where the seq equals
the position-1 of the prim and sec nodes within the customers tag. i.e
prim is at position 1 and cust is at position 2.

Therefore the primary (prim) customer would have a name of Jon and the
secondary (sec) customer would have a name of peter.
<custs>
<cust>
<seq>0</seq>
<name>jon</name>
</cust>
<cust>
<seq>1</seq>
<name>peter</name>
</cust>
<contacts>
<prim>
<message>hello</message>
</prim>
<sec>
<message>hello</message>
</sec>
</contacts>
</custs>

Oct 15 '05 #3
Eddy C wrote:
Sorry Peter, I'll try and be a little clearer. There was a mistake in
the XML, which you correclty identified.

The idea is to fetch the name from the cust node, where the seq equals
the position-1 of the prim and sec nodes within the customers tag. i.e
prim is at position 1 and cust is at position 2.

Therefore the primary (prim) customer would have a name of Jon and the
secondary (sec) customer would have a name of peter.
<custs>
<cust>
<seq>0</seq>
<name>jon</name>
</cust>
<cust>
<seq>1</seq>
<name>peter</name>
</cust>
<contacts>
<prim>
<message>hello</message>
</prim>
<sec>
<message>hi</message>
</sec>
</contacts>
</custs>


OK, but if you are already distinguishing between prim and sec, and you know
they map directly to seq=0 and seq=1 respectively, I don't see the problem:

<xsl:choose>
<xsl:when test="name()='prim'">
<xsl:value-of select="//cust[seq=0]/name"/>
</xsl:when>
<xsl:when test="name()='sec'">
<xsl:value-of select="//cust[seq=1]/name"/>
</xsl:when>
</xsl:choose>

The only reason for doing it by counting would be if you had far more
(other) element content in contacts or cust than you are showing us.
In which case the content of contacts would surely not be prim and sec
but some other element which could be enumerated. Usually the reason for
naming element types a certain way is because it describes *different*
types of data, whereas here the element content of prim and sec is
identical.

This appears to be a peculiarly obtuse piece of design. Why not

<customers>
<customer n="0" type="primary" name="jon" message="hello"/>
<customer n="1" type="secondary" name="peter" message="hello"/>
</customers>

By all means give the customer element subelement content if the character
data content of name and message is more complex, but I see no value in
segmenting the primary/secondary classification from the customer data.

///Peter
Oct 16 '05 #4
Peter,

Its not my design, I'm just the consumer and needed to find a way to
come up with a single XPath query to get the associated bit of data.

Also the example I gave is very stripped down. Looking at the Schema
doc prim and sec have very different attributes but the record we are
trying to join to has the same attributes for both.

So it looks like its not going to be simple to do this in one single
XPath query.

Oct 17 '05 #5
Eddy C wrote:
Peter,

Its not my design, I'm just the consumer and needed to find a way to
come up with a single XPath query to get the associated bit of data.

Also the example I gave is very stripped down. Looking at the Schema
doc prim and sec have very different attributes but the record we are
trying to join to has the same attributes for both.

So it looks like its not going to be simple to do this in one single
XPath query.


OK, if it has to be done that way...
If your current element is either prim or sec, then

/custs/cust[seq=count(current()/preceding-sibling::*)]/name

will return the name of the matching customer.

///Peter
Oct 17 '05 #6
Very nice, I really appreciate your help.

Oct 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
5
by: reddy | last post by:
I am trying to insert a node into an XMLFile. using XMLTextwriter. My Question is Is it possible to do without using XMLDocument. Because its loading all the the file into memory. I just want to...
15
by: TJ Walls | last post by:
Hello All, Is it possible to create a <select> element with no selected options? I have tried setting the selectedIndex attribute to -1, but as far as I can tell this only works for <select...
2
by: Cali | last post by:
Please bear with me, I have been reading about XSL for a couple hours. I have an XML document that contains multiple <page> tags interspersed throughout the tree. <text> .... <page>1</page>...
3
by: Matthew Woods | last post by:
Hi, does anyone know how to select a treenode with the first click? the first click on a treeview will highlight the node under the mouse when it was clicked BUT that node isn't actually selected...
3
by: Andy | last post by:
Hi, I'm trying to render tabular data in an HTML document using XSL to transform XML data into an HTML table. Some of the tabular data appears as droplists (implemented by the HTML Select and...
2
by: naima.mans | last post by:
Hello, i want to select 2 following brothers nodes wich are one under another (one closed to another)... i have done one xslt file... but it's not really good.. for example: the xml file:...
1
by: Zachary Turner | last post by:
I want to select some nodes based on whether or not another node exists in another part of the document with the exact same name. For example, <special-location> <special-element...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.