467,920 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

numbering across different elements

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
Jul 20 '05 #1
  • viewed: 1549
Share:
10 Replies
In article <72**************************@posting.google.com >,
Mike Dickens <mi**@progress-partnership.co.uk> 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
Jul 20 '05 #2
On 27 Oct 2004 09:06:13 -0700, mi**@progress-partnership.co.uk (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
Jul 20 '05 #3
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.
Jul 20 '05 #4
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.
mi**@progress-partnership.co.uk (Mike Dickens) wrote in message news:<72**************************@posting.google. 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

Jul 20 '05 #5
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.
Jul 20 '05 #6
Hi Mike,

Try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 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" <mi**@progress-partnership.co.uk> wrote in message
news:72**************************@posting.google.c 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.
mi**@progress-partnership.co.uk (Mike Dickens) wrote in message

news:<72**************************@posting.google. 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

Jul 20 '05 #7
In article <72**************************@posting.google.com >,
Mike Dickens <mi**@progress-partnership.co.uk> 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
Jul 20 '05 #8
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.
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote in message news:<cl***********@pc-news.cogsci.ed.ac.uk>...
In article <72**************************@posting.google.com >,
Mike Dickens <mi**@progress-partnership.co.uk> 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

Jul 20 '05 #9
In article <72**************************@posting.google.com >,
Mike Dickens <mi**@progress-partnership.co.uk> 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
Jul 20 '05 #10
Mike Dickens <mi**@progress-partnership.co.uk> 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?


If you can compile a Bash shell, then

start () {
local "$@" # b i=Y j=aaaa
[ "$i" = Y ] && echo $((++count)). $j
}
xml -s start "`< file.xml`"

will print

1. aaaa
2. cccc
3. ffff

Ref:
http://freshmeat.net/projects/bashdiff/

--
William Park <op**********@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
Jul 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dwayne Wilkinson | last post: by
2 posts views Thread by Mike Dickens | last post: by
6 posts views Thread by Stanimir Stamenkov | last post: by
6 posts views Thread by Christian Roth | last post: by
15 posts views Thread by bg_ie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.