DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: How to search for an attribute w/o xsl

  1. #1
    Candy Guest

    How to search for an attribute w/o xsl


    Hi,

    How does one search through an xml document looking for an id attribute of
    an element? I have a form and it allows you to create and append records
    into an xml document. The user has to enter an id for each record and I was
    just wondering what the syntax is for looking up all the id's of all the
    records?

    I am also using ASP **BUT I'M LOOKING FOR XML SYNTAX**

    I would like to pull the form's id and compare it to all the ids in the xml
    document.

    The browser automatically complains if the user enters an already used id
    but i would like to provide a more user friendly error comment.

    Any Suggestions?
    Thanks,
    Candy

  2. #2
    MarkN Guest

    Re: How to search for an attribute w/o xsl


    Use a parser.

  3. #3
    Candy Guest

    Re: How to search for an attribute w/o xsl


    "MarkN" <mnuttall@nospam.com> wrote:
    >
    >Use a parser.


    If i use the msxml parser..will it give its own error messages? How do I
    use it to give the error messages I want?

    Thanks

    Candy

  4. #4
    MarkN Guest

    Re: How to search for an attribute w/o xsl


    1. It may throw errors depending on what you are doing.
    2. Catch the error and display something (response.write) or raise your
    own (err.raise "Something bad happened" - may not work in ASP ???)

    Mark


    "Candy" <candy@apsc.ubc.ca> wrote:
    >
    >"MarkN" <mnuttall@nospam.com> wrote:
    >>
    >>Use a parser.

    >
    >If i use the msxml parser..will it give its own error messages? How do I
    >use it to give the error messages I want?
    >
    >Thanks
    >
    >Candy



  5. #5
    Candy Guest

    Re: How to search for an attribute w/o xsl


    Hi,
    I know what the error is but I don't know how to catch it.
    The user is adding to an xml document. The error is that the user is entering
    an id that is already used in the xml doc. How do i search the xml doc myself(i
    suppose)--if id is in xml doc response.write "enter new id"-- Is it possible
    to search the xml doc like this? Or if there is a way to do this with the
    parser you may have to explicitly tell me bc I am such a newbie

    Thanks to all,
    Candy


    "MarkN" <mnuttall@nospam.com> wrote:
    >
    >1. It may throw errors depending on what you are doing.
    >2. Catch the error and display something (response.write) or raise your
    >own (err.raise "Something bad happened" - may not work in ASP ???)
    >
    >Mark
    >
    >
    >"Candy" <candy@apsc.ubc.ca> wrote:
    >>
    >>"MarkN" <mnuttall@nospam.com> wrote:
    >>>
    >>>Use a parser.

    >>
    >>If i use the msxml parser..will it give its own error messages? How do

    I
    >>use it to give the error messages I want?
    >>
    >>Thanks
    >>
    >>Candy

    >



  6. #6
    MarkN Guest

    Re: How to search for an attribute w/o xsl



    Not sure how the XML is throwing that kind of error. You just need to look
    for it. To search XML you should use a parser. Without seeing your xml
    I can only give examples. Is the id an element(<ID></ID>) or attribute(<PERSON
    ID="JOE"><PERSON>)? And why does this need to be in XML? XML is best for
    communicating between different systems/apps/etc. It is not really the best
    for storage/searching unless maybe you use an XML database.

    Mark


  7. #7
    Russell Jones Guest

    Re: How to search for an attribute w/o xsl

    Try this to find out if an element with a matching id value exists--this
    code uses the MS parser and VB, but if you're using something else, you may
    be able to glean enough from it to move forward. Assume the new ID entered
    by the user is in the variable anID, and that it's a string, and that your
    XML document is successfully loaded into a DOMDocument object variable named
    doc.
    Dim anID As String
    anID = <user-entered-id>
    Dim N as IXMLDOMNode
    Set N = doc.selectSingleNode(//*[@id='" & anID & "']")
    If not N is Nothing then
    ' an element with that ID already exists
    Else
    ' do something here
    End If

    If you're looking for an element named id rather than an attribute, change
    the XPath query to:
    Set N = doc.selectSingleNode(//id[text()='" & anID & "']")

    The XPath queries shown in this code check every node for one with a
    matching ID value stopping at the first match. If you can make the query
    more explicit, you can speed up the search.

    HTH
    Russell Jones
    Sr. Web Development Editor,
    DevX.com


    "Candy" <candy@apsc.ubc.ca> wrote in message
    news:3be9770e@147.208.176.211...
    >
    > Hi,
    > I know what the error is but I don't know how to catch it.
    > The user is adding to an xml document. The error is that the user is

    entering
    > an id that is already used in the xml doc. How do i search the xml doc

    myself(i
    > suppose)--if id is in xml doc response.write "enter new id"-- Is it

    possible
    > to search the xml doc like this? Or if there is a way to do this with the
    > parser you may have to explicitly tell me bc I am such a newbie
    >
    > Thanks to all,
    > Candy
    >
    >
    > "MarkN" <mnuttall@nospam.com> wrote:
    > >
    > >1. It may throw errors depending on what you are doing.
    > >2. Catch the error and display something (response.write) or raise your
    > >own (err.raise "Something bad happened" - may not work in ASP ???)
    > >
    > >Mark
    > >
    > >
    > >"Candy" <candy@apsc.ubc.ca> wrote:
    > >>
    > >>"MarkN" <mnuttall@nospam.com> wrote:
    > >>>
    > >>>Use a parser.
    > >>
    > >>If i use the msxml parser..will it give its own error messages? How do

    > I
    > >>use it to give the error messages I want?
    > >>
    > >>Thanks
    > >>
    > >>Candy

    > >

    >




  8. #8
    MarkN Guest

    Re: How to search for an attribute w/o xsl


    Good Job. I could have just answered the question too, but I really
    wanted to have her think about what she was doing (being a newbie and all).
    I do appreciate the code though.

  9. #9
    Candy Guest

    Re: How to search for an attribute w/o xsl


    Oh My Gosh!!! I can't believe its that easy...why i didn't see it.
    Thanks to you both.

    Cheers,

    Candy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center