Using xPath to Search Entire xml file

Discussion in 'XML' started by ETL, Dec 29, 2004.

  1. ETL

    ETL Guest

    Hi,
    I have an xml document that feeds a treeview menu on my web site. The
    structure of the xml file is as follows.

    <XML type="text/xml">
    <root>
    <a0 name="One">
    <a0_0 html="Report 1" id="1" />
    <a0_1 html="Report 2" id="2" />
    <a0_2 html="Report 3" id="3" />
    </a0>
    <a1 name="Two">
    <a1_0 html="Report 1" id="4" />
    <a1_1 html="Report 2" id="5" />
    <a1_2 html="Report 3" id="6" />
    </a1>
    <a2 name="Three">
    <a2_1 name="Three_One">
    <a2_1_1 html="Report 1" id="7" />
    <a2_2_2 html="Report 2" id="8" />
    </a2_1>
    <a2_2 name="Three_Two">
    <a2_2_1 html="Email Report 1" id="9" />
    <a2_2_2 html="Email Report 2" id="10" />
    <a2_2_3 html="Email Report 3" id="11" />
    </a2_2>
    </a2>
    </root>
    </XML>

    I want to buld an asp.net application that takes in a node id attribute
    and returns the html attribute of the desired node. So if 11 were
    passed in it would return "Email Report 3".

    I've found lots of xPath examples that search a straigh aligned xml
    file that doesn't have nested attributes but my file is more complex
    and includes tierded menu folders.

    How can I search the entire document for the ID attribute.

    One other thing, is there a difference in xPath quesries when using
    attribute or element based xml? Just curious.
    Thanks for your help.

    Regards

    Eric
     
    ETL, Dec 29, 2004
    #1
    1. Advertising

  2. ETL wrote:


    > I have an xml document that feeds a treeview menu on my web site. The
    > structure of the xml file is as follows.
    >
    > <XML type="text/xml">
    > <root>
    > <a0 name="One">
    > <a0_0 html="Report 1" id="1" />
    > <a0_1 html="Report 2" id="2" />
    > <a0_2 html="Report 3" id="3" />
    > </a0>
    > <a1 name="Two">
    > <a1_0 html="Report 1" id="4" />
    > <a1_1 html="Report 2" id="5" />
    > <a1_2 html="Report 3" id="6" />
    > </a1>
    > <a2 name="Three">
    > <a2_1 name="Three_One">
    > <a2_1_1 html="Report 1" id="7" />
    > <a2_2_2 html="Report 2" id="8" />
    > </a2_1>
    > <a2_2 name="Three_Two">
    > <a2_2_1 html="Email Report 1" id="9" />
    > <a2_2_2 html="Email Report 2" id="10" />
    > <a2_2_3 html="Email Report 3" id="11" />
    > </a2_2>
    > </a2>
    > </root>
    > </XML>
    >
    > I want to buld an asp.net application that takes in a node id attribute
    > and returns the html attribute of the desired node. So if 11 were
    > passed in it would return "Email Report 3".


    Having the nesting level encoded in the element name is a pretty odd way
    and will cause you a lot of trouble I think.
    The XPath could be
    //*[@id = '11']/@html

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 29, 2004
    #2
    1. Advertising

  3. ETL

    Joris Gillis Guest

    >> I want to buld an asp.net application that takes in a node id attribute
    >> and returns the html attribute of the desired node. So if 11 were
    >> passed in it would return "Email Report 3".

    >
    > Having the nesting level encoded in the element name is a pretty odd way
    > and will cause you a lot of trouble I think.
    > The XPath could be
    > //*[@id = '11']/@html
    >


    Maybe you could use the DOM function 'getElementById' instead of an Xpath expression?


    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Vincit omnia simplicitas
     
    Joris Gillis, Dec 29, 2004
    #3
  4. ETL

    ETL Guest

    Joris Gillis wrote:
    > >> I want to buld an asp.net application that takes in a node id

    attribute
    > >> and returns the html attribute of the desired node. So if 11 were
    > >> passed in it would return "Email Report 3".

    > >
    > > Having the nesting level encoded in the element name is a pretty

    odd way
    > > and will cause you a lot of trouble I think.
    > > The XPath could be
    > > //*[@id = '11']/@html
    > >

    >
    > Maybe you could use the DOM function 'getElementById' instead of an

    Xpath expression?
    >
    >
    > --
    > Joris Gillis

    (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    > Vincit omnia simplicitas


    I'm not so sure that the 'getElementById' format works for nested
    nodes. All the exmples I found for this used simple one dimensional XML
    documents.
     
    ETL, Dec 29, 2004
    #4
  5. ETL

    ETL Guest

    Joris Gillis wrote:
    > >> I want to buld an asp.net application that takes in a node id

    attribute
    > >> and returns the html attribute of the desired node. So if 11 were
    > >> passed in it would return "Email Report 3".

    > >
    > > Having the nesting level encoded in the element name is a pretty

    odd way
    > > and will cause you a lot of trouble I think.
    > > The XPath could be
    > > //*[@id = '11']/@html
    > >

    >
    > Maybe you could use the DOM function 'getElementById' instead of an

    Xpath expression?
    >
    >
    > --
    > Joris Gillis

    (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    > Vincit omnia simplicitas


    I'm not so sure that the 'getElementById' format works for nested
    nodes. All the exmples I found for this used simple one dimensional XML
    documents.
     
    ETL, Dec 29, 2004
    #5
  6. Joris Gillis wrote:

    >> Having the nesting level encoded in the element name is a pretty odd way
    >> and will cause you a lot of trouble I think.
    >> The XPath could be
    >> //*[@id = '11']/@html
    >>

    >
    > Maybe you could use the DOM function 'getElementById' instead of an
    > Xpath expression?


    If you want to use getElementById then the document needs a DTD that
    defines that the attributes of name id are of type ID. The original
    example doesn't have a DTD. And with those element names encoding
    nesting levels the DTD would need to be adjusted every time the nesting
    level of the XML increases.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 30, 2004
    #6
  7. ETL

    Joris Gillis Guest

    > If you want to use getElementById then the document needs a DTD that
    > defines that the attributes of name id are of type ID.

    I'll try not to make that mistake again...

    > The original example doesn't have a DTD. And with those element names
    > encoding nesting levels the DTD would need to be adjusted every time the
    > nesting level of the XML increases.


    Is it really necessary to define the whole DTD? Can one not specify the
    attribute that is of type ID without including any other document
    definition? (note that I've never used DTD, XML Schema or RelaxNG)

    regards,
    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Joris Gillis, Dec 30, 2004
    #7
  8. Joris Gillis wrote:

    > Is it really necessary to define the whole DTD? Can one not specify the
    > attribute that is of type ID without including any other document
    > definition?


    With a DTD you need to define the attribute for each element you want to
    use it on.
    I am not sure what is supposed to happen if you do not define the
    elements but then specify attributes but it is certainly not reliable
    thing to do.
    In my tests here with MSXML 3 and with .NET XmlDocument elements are not
    found by id (using the nodeFromID function in MSXML and GetElementById
    with .NET) if only attributes are defined.
    Mozilla with getElementById finds the elements if only the attributes
    are defined. But Mozilla only processes internal subsets of DTDs so in
    many cases you can't rely on ID attributes and getElementById.


    There is a new W3C recommendation for the xml:id attribute
    <http://www.w3.org/TR/xml-id/>
    if that is going to be implemented then no DTD should be required to
    make use of getElementById.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 30, 2004
    #8
  9. ETL

    Joris Gillis Guest

    > There is a new W3C recommendation for the xml:id attribute
    > <http://www.w3.org/TR/xml-id/>
    > if that is going to be implemented then no DTD should be required to
    > make use of getElementById.


    That sounds really interesting. I hope it will soon become a recommendation...

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Scio me nihil scire" - Socrates
     
    Joris Gillis, Dec 30, 2004
    #9
  10. ETL

    ETL Guest

    Joris Gillis wrote:
    > > There is a new W3C recommendation for the xml:id attribute
    > > <http://www.w3.org/TR/xml-id/>
    > > if that is going to be implemented then no DTD should be required

    to
    > > make use of getElementById.

    >
    > That sounds really interesting. I hope it will soon become a

    recommendation...
    >
    > --
    > Joris Gillis

    (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    > "Scio me nihil scire" - Socrates


    lame....it's looking like GetElementByID is going to be too much
    overhead for this application. I don't want to have to maintain a DTD
    for this. Back to xPath solution I guess.....
     
    ETL, Dec 30, 2004
    #10
    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. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,028
    jan V
    Jul 29, 2005
  2. Replies:
    5
    Views:
    6,221
    Andy Dingley
    May 10, 2006
  3. Replies:
    6
    Views:
    964
    Peter Flynn
    Jul 3, 2006
  4. Tharanga Abeyseela

    delete xml elements - using xpath search

    Tharanga Abeyseela, Oct 16, 2012, in forum: Python
    Replies:
    0
    Views:
    179
    Tharanga Abeyseela
    Oct 16, 2012
  5. Stefan Behnel
    Replies:
    0
    Views:
    171
    Stefan Behnel
    Oct 16, 2012
Loading...

Share This Page