Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Specify an element in a nodeset directly

Reply
Thread Tools

Specify an element in a nodeset directly

 
 
eric.goforth@gmail.com
Guest
Posts: n/a
 
      10-02-2006
Hello,

Is there any way to directly access an element in a nodeset?

For example, if working with:

<blahs rec_count="16">
<blah>
<yada>abc</yada>
</blah>
<blah>
<yada>123</yada>
</blah>
<blah>
<yada>xyz</yada>
</blah>
</blahs>

For instance, I could specify something like blahs/blah[2]/yada and get
the value of "123"?

-Eric

 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      10-02-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello,
>
> Is there any way to directly access an element in a nodeset?
>
> For example, if working with:
>
> <blahs rec_count="16">
> <blah>
> <yada>abc</yada>
> </blah>
> <blah>
> <yada>123</yada>
> </blah>
> <blah>
> <yada>xyz</yada>
> </blah>
> </blahs>
>
> For instance, I could specify something like blahs/blah[2]/yada and get
> the value of "123"?


Yes, exactly that. In XSLT, for example, you would write

<xsl:value-of select="blahs/blah[2]/yada"/>

But this doesn't have anything to do with nodesets.
It's just an XPath statement referencing an element.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
 
Reply With Quote
 
 
 
 
eric.goforth@gmail.com
Guest
Posts: n/a
 
      10-02-2006

Peter Flynn wrote:
> (E-Mail Removed) wrote:
> > Hello,
> >
> > Is there any way to directly access an element in a nodeset?
> >
> > For example, if working with:
> >
> > <blahs rec_count="16">
> > <blah>
> > <yada>abc</yada>
> > </blah>
> > <blah>
> > <yada>123</yada>
> > </blah>
> > <blah>
> > <yada>xyz</yada>
> > </blah>
> > </blahs>
> >
> > For instance, I could specify something like blahs/blah[2]/yada and get
> > the value of "123"?

>
> Yes, exactly that. In XSLT, for example, you would write
>
> <xsl:value-of select="blahs/blah[2]/yada"/>
>
> But this doesn't have anything to do with nodesets.
> It's just an XPath statement referencing an element.
>


Peter,

Hmmm, well the situation in a little more detail is that I'm passing in
this nodeset, I believe that's the right word, in a parameter.

So my entire XML document looks more like:

<mydoc>
<mystuff>
<blahs rec_count="3">
<blah>
<yada>abc</yada>
</blah>
<blah>
<yada>123</yada>
</blah>
<blah>
<yada>xyz</yada>
</blah>
</blahs>
</mystuff>
</mydoc>

I'm passing the blahs to a template as a parameter like so:


<xsl:apply-templates select="somestuff">
<xsl:with-param name="myblahs"
select="/mydoc/mystuff/blahs"></xsl:with-param>
</xsl:apply-templates>


Then in my template it looks something like:

<xsl:template match="somestuff">

<xslaram name="myblahs"/>

.... Do some stuff...

</xsl:template >

So debugging the transformation of the somestuff template in XML Spy,
if I look at $myblahs in the XPath-Watch, I see NodeSet[1]. If I click
on the NodeSet[1] beside the $myblahs in the XPath-Watch I see blahs
with a rec_count="3", so my data made it inside the somestuff template.

However, if I do a <xsl:value-of select="$myblahs/blah[2]/yada"/>

I get an Error: Invalid XPath.

Thanks,
Eric

 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      10-03-2006
> However, if I do a <xsl:value-of select="$myblahs/blah[2]/yada"/>

Try:

$myblahs[2]/yada


Cheers,
Dimitre Novatchev

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> Peter Flynn wrote:
>> (E-Mail Removed) wrote:
>> > Hello,
>> >
>> > Is there any way to directly access an element in a nodeset?
>> >
>> > For example, if working with:
>> >
>> > <blahs rec_count="16">
>> > <blah>
>> > <yada>abc</yada>
>> > </blah>
>> > <blah>
>> > <yada>123</yada>
>> > </blah>
>> > <blah>
>> > <yada>xyz</yada>
>> > </blah>
>> > </blahs>
>> >
>> > For instance, I could specify something like blahs/blah[2]/yada and get
>> > the value of "123"?

>>
>> Yes, exactly that. In XSLT, for example, you would write
>>
>> <xsl:value-of select="blahs/blah[2]/yada"/>
>>
>> But this doesn't have anything to do with nodesets.
>> It's just an XPath statement referencing an element.
>>

>
> Peter,
>
> Hmmm, well the situation in a little more detail is that I'm passing in
> this nodeset, I believe that's the right word, in a parameter.
>
> So my entire XML document looks more like:
>
> <mydoc>
> <mystuff>
> <blahs rec_count="3">
> <blah>
> <yada>abc</yada>
> </blah>
> <blah>
> <yada>123</yada>
> </blah>
> <blah>
> <yada>xyz</yada>
> </blah>
> </blahs>
> </mystuff>
> </mydoc>
>
> I'm passing the blahs to a template as a parameter like so:
>
>
> <xsl:apply-templates select="somestuff">
> <xsl:with-param name="myblahs"
> select="/mydoc/mystuff/blahs"></xsl:with-param>
> </xsl:apply-templates>
>
>
> Then in my template it looks something like:
>
> <xsl:template match="somestuff">
>
> <xslaram name="myblahs"/>
>
> ... Do some stuff...
>
> </xsl:template >
>
> So debugging the transformation of the somestuff template in XML Spy,
> if I look at $myblahs in the XPath-Watch, I see NodeSet[1]. If I click
> on the NodeSet[1] beside the $myblahs in the XPath-Watch I see blahs
> with a rec_count="3", so my data made it inside the somestuff template.
>
> However, if I do a <xsl:value-of select="$myblahs/blah[2]/yada"/>
>
> I get an Error: Invalid XPath.
>
> Thanks,
> Eric
>



 
Reply With Quote
 
eric.goforth@gmail.com
Guest
Posts: n/a
 
      10-03-2006

Dimitre Novatchev wrote:
> > However, if I do a <xsl:value-of select="$myblahs/blah[2]/yada"/>

>
> Try:
>
> $myblahs[2]/yada
>
>
> Cheers,
> Dimitre Novatchev
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> >
> > Peter Flynn wrote:
> >> (E-Mail Removed) wrote:
> >> > Hello,
> >> >
> >> > Is there any way to directly access an element in a nodeset?
> >> >
> >> > For example, if working with:
> >> >
> >> > <blahs rec_count="16">
> >> > <blah>
> >> > <yada>abc</yada>
> >> > </blah>
> >> > <blah>
> >> > <yada>123</yada>
> >> > </blah>
> >> > <blah>
> >> > <yada>xyz</yada>
> >> > </blah>
> >> > </blahs>
> >> >
> >> > For instance, I could specify something like blahs/blah[2]/yada and get
> >> > the value of "123"?
> >>
> >> Yes, exactly that. In XSLT, for example, you would write
> >>
> >> <xsl:value-of select="blahs/blah[2]/yada"/>
> >>
> >> But this doesn't have anything to do with nodesets.
> >> It's just an XPath statement referencing an element.
> >>

> >
> > Peter,
> >
> > Hmmm, well the situation in a little more detail is that I'm passing in
> > this nodeset, I believe that's the right word, in a parameter.
> >
> > So my entire XML document looks more like:
> >
> > <mydoc>
> > <mystuff>
> > <blahs rec_count="3">
> > <blah>
> > <yada>abc</yada>
> > </blah>
> > <blah>
> > <yada>123</yada>
> > </blah>
> > <blah>
> > <yada>xyz</yada>
> > </blah>
> > </blahs>
> > </mystuff>
> > </mydoc>
> >
> > I'm passing the blahs to a template as a parameter like so:
> >
> >
> > <xsl:apply-templates select="somestuff">
> > <xsl:with-param name="myblahs"
> > select="/mydoc/mystuff/blahs"></xsl:with-param>
> > </xsl:apply-templates>
> >
> >
> > Then in my template it looks something like:
> >
> > <xsl:template match="somestuff">
> >
> > <xslaram name="myblahs"/>
> >
> > ... Do some stuff...
> >
> > </xsl:template >
> >
> > So debugging the transformation of the somestuff template in XML Spy,
> > if I look at $myblahs in the XPath-Watch, I see NodeSet[1]. If I click
> > on the NodeSet[1] beside the $myblahs in the XPath-Watch I see blahs
> > with a rec_count="3", so my data made it inside the somestuff template.
> >
> > However, if I do a <xsl:value-of select="$myblahs/blah[2]/yada"/>
> >
> > I get an Error: Invalid XPath.
> >
> > Thanks,
> > Eric
> >


I've figured out how to do get it to work. <xsl:value-of
select="$myblahs/blah[2]/yada"/> is working for me now, I think that I
must have had a type in there.

The only problem I'm having now is figuring out how to make the "2"
variable.

For instance:

<xsl:value-of
select="$myblahs/blah[/xpath1/xpath2/someintegervalue]/yada"/>

-Eric

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      10-03-2006


(E-Mail Removed) wrote:


> <xsl:value-of
> select="$myblahs/blah[/xpath1/xpath2/someintegervalue]/yada"/>


$myblahs/blah[/xpath1/xpath2/someintegervalue = position()]/yada

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
eric.goforth@gmail.com
Guest
Posts: n/a
 
      10-03-2006

Martin Honnen wrote:
> (E-Mail Removed) wrote:
>
>
> > <xsl:value-of
> > select="$myblahs/blah[/xpath1/xpath2/someintegervalue]/yada"/>

>
> $myblahs/blah[/xpath1/xpath2/someintegervalue = position()]/yada
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/


Martin,

Hmmm, that's not working for me. If I put in a "1" or a "2" it works,
but not with the "variable".

In my case

<xsl:apply-templates select="thingsummary">
<xsl:with-param name="furballs"
select="/xpath1/xpath2/furballssummary"></xsl:with-param>
</xsl:apply-templates>

</xsl:template>

<xsl:template match="thingsummary">

<xslaram name="furballs"/>

<xsl:apply-templates select="thing[(position() mod 2)=1]">
<xsl:with-param name="furballs"
select="$furballs"></xsl:with-param>
</xsl:apply-templates>

</xsl:template>

<xsl:template match="thing[(position() mod 2)=1]">

<xslaram name="furballs"/>

<!-- This works-->
<xsl:value-of select="thingintegerfield1"/>

<!-- This works-->
<xsl:value-of select="$furballs/furball[1]/furballmember"/>

<!-- This works-->
<xsl:value-of select="$furballs/furball[1=
position()]/furballmember"/>

<!-- This does not work-->
<xsl:value-of
select="$furballs/furball[1=thingintegerfield1]/furballmember"/>

</xsl:template>

Thanks
Eric

 
Reply With Quote
 
roy axenov
Guest
Posts: n/a
 
      10-03-2006

(E-Mail Removed) wrote:
> Martin Honnen wrote:
> > (E-Mail Removed) wrote:
> > > <xsl:value-of select="
> > > $myblahs/blah[/xpath1/xpath2/someintegervalue]/yada
> > > "/>

> >
> > $myblahs/blah
> > [/xpath1/xpath2/someintegervalue=position()]/yada

>
> Hmmm, that's not working for me. If I put in a "1" or a
> "2" it works, but not with the "variable".


Your example is awfully vague. For better results, provide
a sample XML and a minimum XSLT demonstrating your problem.

> <xsl:template match="thing[(position() mod 2)=1]">
> <xslaram name="furballs"/>
> <!-- This works-->
> <xsl:value-of select="thingintegerfield1"/>
> <!-- This does not work-->
> <xsl:value-of select="
> $furballs/furball[1=thingintegerfield1]/furballmember
> "/>
> </xsl:template>


First of all, the [1=thingintegerfield1] predicate should
be [position()=thingintegerfield1] if I understood
correctly what you're trying to do (it's hard to be sure
without a working example).

Even then, I'd say you don't understand how contexts work.
Let's assume this template matched the node
/foo[1]/bar[1]/thing[1]. What does thingintegerfield1 in
the XPath marked as 'This works' refer to then? The answer
is '/foo[1]/bar[1]/thing[1]/thingintegerfield1'. And what
does thingintegerfield1 in the XPath marked as 'This does
not work' refer to? '$furballs/furball/thingintegerfield1'.
What you probably need is current()/thingintegerfield1,
although, once again, it's hard to be sure without a
working example.

To demonstrate:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar>
<xyzzy>
<int>3</int>
</xyzzy>
</bar>
<nodeset>
<node>
<str>wrong (1)</str>
<int>3</int>
</node>
<node>
<str>wrong (2)</str>
<int>2</int>
</node>
<node>
<str>right (3)</str>
<int>1</int>
</node>
</nodeset>
</foo>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="descendant:yzzy">
<xsl:with-param name="ns" select="/foo/nodeset"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="/foo/bar/xyzzy">
<xslaram name="ns"/>
My Int: <xsl:value-of select="int"/>
Wrong: <xsl:value-of select="
$ns/node[int=position()]/str"/>
Right: <xsl:value-of select="
$ns/node[current()/int=position()]/str"/>
</xsl:template>
</xsl:stylesheet>

--
roy axenov

 
Reply With Quote
 
eric.goforth@gmail.com
Guest
Posts: n/a
 
      10-03-2006

roy axenov wrote:
> (E-Mail Removed) wrote:
> > Martin Honnen wrote:
> > > (E-Mail Removed) wrote:
> > > > <xsl:value-of select="
> > > > $myblahs/blah[/xpath1/xpath2/someintegervalue]/yada
> > > > "/>
> > >
> > > $myblahs/blah
> > > [/xpath1/xpath2/someintegervalue=position()]/yada

> >
> > Hmmm, that's not working for me. If I put in a "1" or a
> > "2" it works, but not with the "variable".

>
> Your example is awfully vague. For better results, provide
> a sample XML and a minimum XSLT demonstrating your problem.
>


Roy,

Here's an example of what I'm referring to. The "does not work"
comment shows what I'm trying to do. The "this does work" shows where
I'm able to hit the nodeset by hardcoding the parameter.

Thanks,
Eric

<page>
<contents>
<ratessummary rec_count="3">
<rate>
<ratecode>1</ratecode>
<myrate>1.1</myrate>
</rate>
<rate>
<ratecode>2</ratecode>
<myrate>1.2</myrate>
</rate>
<rate>
<ratecode>3</ratecode>
<myrate>1.3</myrate>
</rate>
</ratessummary>
<thingsummary rec_count="3">
<thing>
<short_description>Jim</short_description>
<description>James</description>
<thingcode>1</thingcode>
</thing>
<thing>
<short_description>Rick</short_description>
<description>Richard</description>
<thingcode>2</thingcode>
</thing>
<thing>
<short_description>Fred</short_description>
<description>Frederick</description>
<thingcode>3</thingcode>
</thing>
</thingsummary>
</contents>
</page>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:apply-templates select="page/contents"></xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="contents">
<table>
<tr>
<td class="data">
<xsl:apply-templates select="thingsummary">
<xsl:with-param name="rates"
select="/page/contents/ratessummary"></xsl:with-param>
</xsl:apply-templates>
</td>
</tr>
</table>
</xsl:template>

<xsl:template match="thingsummary">

<xslaram name="rates"/>

<xsl:value-of select="$rates/rate[1]/org_id"/>

<table>
<xsl:apply-templates select="thing[(position() mod 2)=1]">
<xsl:with-param name="rates" select="$rates"></xsl:with-param>
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="thing[(position() mod 2)=1]">
<xslaram name="rates"/>
<tr>
<td>
<xsl:value-of select="short_description"/>-<xsl:value-of
select="thingcode"/>-<xsl:value-of select="$rates/rate[1]/org_id"/>
<!-- This does not work-->
test1<xsl:value-of select="$rates/rate[position()=thingcode
]/myrate"/>
<!-- This works-->
test2<xsl:value-of select="$rates/rate[position()=1]/myrate"/>
</td>
<xsl:apply-templates select="following-sibling::thing[1]">
<xsl:with-param name="rates" select="$rates"></xsl:with-param>
</xsl:apply-templates>
</tr>
</xsl:template>
<xsl:template match="thing[(position() mod 2)=0]">
<xslaram name="rates"/>
<td>
<xsl:value-of select="short_description"/>-<xsl:value-of
select="thingcode"/><xsl:value-of select="$rates/rate[1]/org_id"/>
<!-- This does not work-->
test1<xsl:value-of select="$rates/rate[position()=thingcode
]/myrate"/>
<!-- This works-->
test2<xsl:value-of select="$rates/rate[1]/myrate"/>
</td>
</xsl:template>

</xsl:stylesheet>

 
Reply With Quote
 
roy axenov
Guest
Posts: n/a
 
      10-04-2006

(E-Mail Removed) wrote:
> roy axenov wrote:
> > (E-Mail Removed) wrote:
> > > Martin Honnen wrote:
> > > > (E-Mail Removed) wrote:
> > > > > <xsl:value-of select="
> > > > > $myblahs/blah
> > > > > [/xpath1/xpath2/someintegervalue]/yada
> > > > > "/>
> > > >
> > > > $myblahs/blah
> > > > [/xpath1/xpath2/someintegervalue=position()]/yada
> > >
> > > Hmmm, that's not working for me. If I put in a "1" or
> > > a "2" it works, but not with the "variable".

> >
> > Your example is awfully vague. For better results,
> > provide a sample XML and a minimum XSLT demonstrating
> > your problem.

>
> Here's an example of what I'm referring to. The "does
> not work" comment shows what I'm trying to do. The "this
> does work" shows where I'm able to hit the nodeset by
> hardcoding the parameter.


You should've read the rest of my post. Your example
confirms that my initial guess was right on money. I ran a
quick test, and the solution I proposed works, too.

> <page>
> <contents>
> <ratessummary rec_count="3">
> <rate>
> <ratecode>1</ratecode>
> <myrate>1.1</myrate>
> </rate>
> <rate>
> <ratecode>2</ratecode>
> <myrate>1.2</myrate>
> </rate>
> <rate>
> <ratecode>3</ratecode>
> <myrate>1.3</myrate>
> </rate>
> </ratessummary>
> <thingsummary rec_count="3">
> <thing>
> <short_description>Jim</short_description>
> <description>James</description>
> <thingcode>1</thingcode>
> </thing>
> <thing>
> <short_description>Rick</short_description>
> <description>Richard</description>
> <thingcode>2</thingcode>
> </thing>
> <thing>
> <short_description>Fred</short_description>
> <description>Frederick</description>
> <thingcode>3</thingcode>
> </thing>
> </thingsummary>
> </contents>
> </page>


[XSLT]

> <xsl:apply-templates select="thingsummary">
> <xsl:with-param name="rates"
> select="/page/contents/ratessummary"></xsl:with-param>
> </xsl:apply-templates>


.. . .

> <!-- This does not work-->
> test1<xsl:value-of
> select="$rates/rate[position()=thingcode]/myrate"/>


So what does it do?

$rates/rate selects a nodeset of <rate> nodes. The
predicate [position()=thingcode] filters this nodeset,
dropping all the nodes that don't have a <thingcode> child
with content that is equal to the position of the <rate>
node in the $rates/rate nodeset. Since there are no <rate>
nodes with <thingcode> children in your document, the
resulting nodeset is, naturally, empty. I already explained
the solution in my previous reply.

--
roy axenov

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
specify arbitrary library directory directly in code? scooterm@hotmail.com Python 1 10-28-2005 01:24 AM
Nodeset Difference AKA Nodeset Reduction Bryan Galvin XML 2 09-29-2004 12:09 PM
Preview image directly on PC, save directly to HD Patrick M. Digital Photography 3 01-07-2004 08:29 PM
Please clarify how null nodeset result processes? Kathy Burke ASP .Net 5 08-26-2003 07:03 PM
PRETTY PLEASE...need answer to why "this method should result in NodeSet" error KathyB ASP .Net 0 07-25-2003 04:59 AM



Advertisments