Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > numbering across different elements

Reply
Thread Tools

numbering across different elements

 
 
Mike Dickens
Guest
Posts: n/a
 
      10-27-2004
hi,
suppose i have:

<a>
<b i="Y" j="aaaa"/>
<c i="N" j="bbbb"/>
<d i="Y" j="cccc"/>
<e i="N" j="dddd"/>
<f i="N" j="eeee"/>
<g i="Y" j="ffff"/>
</a>

and i want to extract the elements where i="Y" such that i get something like
<x>
<y>1. aaaa</y>
<y>2. cccc</y>
<y>3. gggg</y>
</x>

how would i get the numbering to work across the different elements?

thanks,
mike
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      10-27-2004
In article <(E-Mail Removed) >,
Mike Dickens <(E-Mail Removed)> wrote:

>and i want to extract the elements where i="Y" such that i get something like
><x>
> <y>1. aaaa</y>
> <y>2. cccc</y>
> <y>3. gggg</y>
></x>
>
>how would i get the numbering to work across the different elements?


Use xsl:number with the "count" attribute to specify which elements
are counted.

-- Richard
 
Reply With Quote
 
 
 
 
Jeff Kish
Guest
Posts: n/a
 
      10-27-2004
On 27 Oct 2004 09:06:13 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Mike Dickens)
wrote:

>hi,
>suppose i have:
>
><a>
> <b i="Y" j="aaaa"/>
> <c i="N" j="bbbb"/>
> <d i="Y" j="cccc"/>
> <e i="N" j="dddd"/>
> <f i="N" j="eeee"/>
> <g i="Y" j="ffff"/>
></a>
>
>and i want to extract the elements where i="Y" such that i get something like
><x>
> <y>1. aaaa</y>
> <y>2. cccc</y>
> <y>3. gggg</y>
></x>
>
>how would i get the numbering to work across the different elements?
>
>thanks,
>mike

mmmm.. you just want to enumerate all of your elements?
What technology is at your disposal?


In XQuery, using books.xml,
this query:

for $t at $i in document("books.xml")//*[@year="1994"]
return <Response>{$i, name($t), string($t/@year)}</Response>

returns this data:

<Response>1 book 1994</Response>


Here is books.xml (from katz_c01.pdf tutorial I found via google at
http://www.datadirect.com/news/whats...book/index.ssp)
<bib>
<book year="1994">a1994 <title>TCP/IP Illustrated</title>
<author>
<last>Stevens</last>
<first>W.</first>
</author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year="1992">a1992<title>Advanced Programming in the UNIX
Environment</title>
<author>
<last>Stevens</last>
<first>W.</first>
</author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year="2000">a2000<title>Data on the Web</title>
<author>
<last>Abiteboul</last>
<first>Serge</first>
</author>
<author>
<last>Buneman</last>
<first>Peter</first>
</author>
<author>
<last>Suciu</last>
<first>Dan</first>
</author>
<publisher>Morgan Kaufmann Publishers</publisher>
<price>65.95</price>
</book>
<book year="1999">a1999<title>The Economics of Technology and Content
for Digital TV</title>
<editor>
<last>Gerbarg</last>
<first>Darcy</first>
<affiliation>CITI</affiliation>
</editor>
<publisher>Kluwer Academic Publishers</publisher>
<price>129.95</price>
</book>
</bib>


Jeff Kish
 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      10-27-2004
Mike Dickens wrote:

> <a>
> <b i="Y" j="aaaa"/>
> <c i="N" j="bbbb"/>
> <d i="Y" j="cccc"/>
> <e i="N" j="dddd"/>
> <f i="N" j="eeee"/>
> <g i="Y" j="ffff"/>
> </a>
>
> and i want to extract the elements where i="Y" such that i get something like


For the sake of comparison, here is a solution in XMLgawk:


BEGIN {
XMLMODE=1
print "<x>"
}

XMLSTARTELEM {
if (XMLATTR["i"] == "Y")
print " <y>" ++n ". " XMLATTR["j"] "</y>"
}

END { print "</x>" }



I tested it and it produced:

<x>
<y>1. aaaa</y>
<y>2. cccc</y>
<y>3. ffff</y>
</x>

> how would i get the numbering to work across the different elements?


You probably mean how to make sure that nodes
at all nesting levels are treated in the same
way. In XMLgawk this is no problem because the
interpreter traverses all nodes anyway.
 
Reply With Quote
 
Mike Dickens
Guest
Posts: n/a
 
      10-28-2004
whoops,
i forgot to mention. i need to do this as an xsl transform. i am
thinking <xsl:number> is probably the way i want to go, but am unsure
as to how to go about it. all the examples i can find refer to
elements of the same type, not amalgamating different types as i want
to do here.

mike.


(E-Mail Removed) (Mike Dickens) wrote in message news:<(E-Mail Removed). com>...
> hi,
> suppose i have:
>
> <a>
> <b i="Y" j="aaaa"/>
> <c i="N" j="bbbb"/>
> <d i="Y" j="cccc"/>
> <e i="N" j="dddd"/>
> <f i="N" j="eeee"/>
> <g i="Y" j="ffff"/>
> </a>
>
> and i want to extract the elements where i="Y" such that i get something like
> <x>
> <y>1. aaaa</y>
> <y>2. cccc</y>
> <y>3. gggg</y>
> </x>
>
> how would i get the numbering to work across the different elements?
>
> thanks,
> mike

 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      10-28-2004
Mike Dickens wrote:

>><a>
>> <b i="Y" j="aaaa"/>
>> <c i="N" j="bbbb"/>
>> <d i="Y" j="cccc"/>
>> <e i="N" j="dddd"/>
>> <f i="N" j="eeee"/>
>> <g i="Y" j="ffff"/>


Notice this one ^^^^

>></a>
>>
>>and i want to extract the elements where i="Y" such that i get something like
>><x>
>> <y>1. aaaa</y>
>> <y>2. cccc</y>
>> <y>3. gggg</y>


I think you have an error in 3.

>></x>
>>


The script that I posted yesterday found this error.
 
Reply With Quote
 
Marrow
Guest
Posts: n/a
 
      10-28-2004
Hi Mike,

Try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml"/>

<xsl:template match="/a">
<xsl:copy>
<xsl:apply-templates select="*[@i = 'Y']"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<y>
<xsl:value-of select="position()"/>
<xsl:text>. </xsl:text>
<xsl:value-of select="@j"/>
</y>
</xsl:template>
</xsl:stylesheet>


HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Mike Dickens" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> whoops,
> i forgot to mention. i need to do this as an xsl transform. i am
> thinking <xsl:number> is probably the way i want to go, but am unsure
> as to how to go about it. all the examples i can find refer to
> elements of the same type, not amalgamating different types as i want
> to do here.
>
> mike.
>
>
> (E-Mail Removed) (Mike Dickens) wrote in message

news:<(E-Mail Removed). com>...
> > hi,
> > suppose i have:
> >
> > <a>
> > <b i="Y" j="aaaa"/>
> > <c i="N" j="bbbb"/>
> > <d i="Y" j="cccc"/>
> > <e i="N" j="dddd"/>
> > <f i="N" j="eeee"/>
> > <g i="Y" j="ffff"/>
> > </a>
> >
> > and i want to extract the elements where i="Y" such that i get something

like
> > <x>
> > <y>1. aaaa</y>
> > <y>2. cccc</y>
> > <y>3. gggg</y>
> > </x>
> >
> > how would i get the numbering to work across the different elements?
> >
> > thanks,
> > mike



 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      10-28-2004
In article <(E-Mail Removed) >,
Mike Dickens <(E-Mail Removed)> wrote:

>i forgot to mention. i need to do this as an xsl transform. i am
>thinking <xsl:number> is probably the way i want to go, but am unsure
>as to how to go about it.


The count attribute is a pattern which matches the elements that
should be counted. So you just need something like:

<xsl:template match="*[@i = 'Y']">
<y>
<xsl:number count="*[@i = 'Y']"/>
<xsl:text>. </xsl:text>
<xsl:value-of select="@j"/>
</y>
</xsl:template>

-- Richard
 
Reply With Quote
 
Mike Dickens
Guest
Posts: n/a
 
      11-03-2004
i think i simplified my example to much. a more accurate file would
be:
<a>
<b a="Y" m="aaaa"/>
<c b="N" n="bbbb"/>
<d c="Y" o="cccc"/>
<e d="N" p="dddd"/>
<f e="N" q="eeee"/>
<g f="Y" r="ffff"/>

this means the is no commonality in the source elements i'm massaging
into a numbered list in the result elements.

in the end i just persuaded the powers that be to do it differently

thanks for the help tho'
mike.


(E-Mail Removed) (Richard Tobin) wrote in message news:<clqu72$30l3$(E-Mail Removed)>...
> In article <(E-Mail Removed) >,
> Mike Dickens <(E-Mail Removed)> wrote:
>
> >i forgot to mention. i need to do this as an xsl transform. i am
> >thinking <xsl:number> is probably the way i want to go, but am unsure
> >as to how to go about it.

>
> The count attribute is a pattern which matches the elements that
> should be counted. So you just need something like:
>
> <xsl:template match="*[@i = 'Y']">
> <y>
> <xsl:number count="*[@i = 'Y']"/>
> <xsl:text>. </xsl:text>
> <xsl:value-of select="@j"/>
> </y>
> </xsl:template>
>
> -- Richard

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      11-03-2004
In article <(E-Mail Removed) >,
Mike Dickens <(E-Mail Removed)> wrote:

>i think i simplified my example to much. a more accurate file would
>be:


There is no problem with this. So long as the pattern
in the count attribute of the sort matches the same nodes as
the match attribute of the template, it will count those elements.

If you are listing elements that have any attribute equal to Y, just
use @*='Y' instead of @i='Y'.

-- Richard
 
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
bit shifts across array elements fermineutron C Programming 6 11-06-2006 02:53 AM
UK phones numbering plan and cost to different prefixes from BT? AKK UK VOIP 4 01-17-2006 12:52 PM
Number hierarchically across heterogeneous elements Mike XML 3 07-07-2005 06:40 PM
numbering across elements Mike Dickens XML 2 10-05-2004 11:28 AM
uniqueness across different child elements Don Bate XML 0 07-22-2003 10:42 PM



Advertisments