numbering across different elements

M

Mike Dickens

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
 
R

Richard Tobin

Mike Dickens said:
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
 
J

Jeff Kish

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/whatsnew/xquerybook/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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

Mike said:
<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>
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.
 
M

Mike Dickens

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.
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

Notice this one ^^^^

I think you have an error in 3.

The script that I posted yesterday found this error.
 
M

Marrow

Hi Mike,

Try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput 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
 
R

Richard Tobin

Mike Dickens said:
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
 
M

Mike Dickens

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.


Mike Dickens said:
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
 
R

Richard Tobin

Mike Dickens said:
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
 
W

William Park

Mike Dickens said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top