numbering across different elements

Discussion in 'XML' started by Mike Dickens, Oct 27, 2004.

  1. Mike Dickens

    Mike Dickens Guest

    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
    Mike Dickens, Oct 27, 2004
    #1
    1. Advertising

  2. In article <>,
    Mike Dickens <> 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
    Richard Tobin, Oct 27, 2004
    #2
    1. Advertising

  3. Mike Dickens

    Jeff Kish Guest

    On 27 Oct 2004 09:06:13 -0700, (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/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
    Jeff Kish, Oct 27, 2004
    #3
  4. 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.
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Oct 27, 2004
    #4
  5. Mike Dickens

    Mike Dickens Guest

    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.


    (Mike Dickens) wrote in message news:<>...
    > 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
    Mike Dickens, Oct 28, 2004
    #5
  6. 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.
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Oct 28, 2004
    #6
  7. Mike Dickens

    Marrow Guest

    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

    "Mike Dickens" <> wrote in message
    news:...
    > 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.
    >
    >
    > (Mike Dickens) wrote in message

    news:<>...
    > > 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
    Marrow, Oct 28, 2004
    #7
  8. In article <>,
    Mike Dickens <> 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
    Richard Tobin, Oct 28, 2004
    #8
  9. Mike Dickens

    Mike Dickens Guest

    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.


    (Richard Tobin) wrote in message news:<clqu72$30l3$>...
    > In article <>,
    > Mike Dickens <> 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
    Mike Dickens, Nov 3, 2004
    #9
  10. In article <>,
    Mike Dickens <> 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
    Richard Tobin, Nov 3, 2004
    #10
  11. Mike Dickens

    William Park Guest

    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?


    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 <>
    Open Geometry Consulting, Toronto, Canada
    William Park, Nov 3, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. buran

    numbering rows in datagrid

    buran, Jul 7, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    468
    Saravana
    Jul 7, 2003
  2. buran

    numbering rows in datagrid

    buran, Oct 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    521
    Cowboy \(Gregory A. Beamer\)
    Oct 15, 2003
  3. =?Utf-8?B?Q2hyaXM=?=

    form field auto numbering with unique value

    =?Utf-8?B?Q2hyaXM=?=, Dec 31, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    438
    John Saunders
    Jan 1, 2005
  4. Don Bate
    Replies:
    0
    Views:
    397
    Don Bate
    Jul 22, 2003
  5. Mike Dickens

    numbering across elements

    Mike Dickens, Oct 4, 2004, in forum: XML
    Replies:
    2
    Views:
    370
    Mike Dickens
    Oct 5, 2004
Loading...

Share This Page