Topic
  • 6 replies
  • Latest Post - ‏2010-03-17T10:06:25Z by SystemAdmin
SaraSmith
SaraSmith
3 Posts

Pinned topic Xpath - How to get all the attribute names and values of an element

‏2010-03-17T07:50:20Z |
Hi All,

I am using xpath in java. I want to get all the attributes (name & Value) of an element. I found the query to get the attribute values of an element, now I want to get attribute names alone or names and values in single query.

For Example,
<Element1 ID=a123 attr1 = value1 attr2 = value2 attr3 = value3 attr4 = value4 attr5 = value5 />

Here using the following query to get all the attribute values of "Element1"
XmlUtils.getAttributes(Path, String.format("//*/@*"));
Using this format //*/@* I can get the values. result would be value1 value2 value3 value4 value5 a123

Now I want to know the query to get all the attribute names, or
query to get all the attributes name and value.

Can anyone help me out to do this, thanks in advance.
Updated on 2010-03-17T10:06:25Z at 2010-03-17T10:06:25Z by SystemAdmin
  • MatthiasNicola
    MatthiasNicola
    47 Posts

    Re: Xpath - How to get all the attribute names and values of an element

    ‏2010-03-17T08:24:50Z  
    Hi Sara,

    you can use the XPath functions name or local-name to obtain attribute or element names:

    //@*/name()

    Getting a list of attribute name/value pairs is slightly more tricky. Try one of the following:

    //@*/(name(), data(.))
    //@*/(name(), string(.))

    For your example, these return the following sequence:
    ID a123 attr1 value1 attr2 value2 attr3 value3 attr4 value4 attr5 value5

    There may well be other solutions.

    Matthias


    Matthias Nicola
    http://www.tinyurl.com/pureXML
    http://nativexmldatabase.com
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath - How to get all the attribute names and values of an element

    ‏2010-03-17T08:37:19Z  
    The XPath expression //*/@* returns a set of attribute nodes. I don't recognize the API you are using to execute the XPath expression, but you will probably find that it returns the result as a NodeList, or a List of Node objects, or something similar. These Node objects will have methods such as getName() and getValue() allowing you to find both the name of the attribute and the value from your calling application.
  • SaraSmith
    SaraSmith
    3 Posts

    Re: Xpath - How to get all the attribute names and values of an element

    ‏2010-03-17T09:34:24Z  
    Hi Sara,

    you can use the XPath functions name or local-name to obtain attribute or element names:

    //@*/name()

    Getting a list of attribute name/value pairs is slightly more tricky. Try one of the following:

    //@*/(name(), data(.))
    //@*/(name(), string(.))

    For your example, these return the following sequence:
    ID a123 attr1 value1 attr2 value2 attr3 value3 attr4 value4 attr5 value5

    There may well be other solutions.

    Matthias


    Matthias Nicola
    http://www.tinyurl.com/pureXML
    http://nativexmldatabase.com
    Hi Matthias,

    Thanks for ur reply,

    I have tried all the 3 formats in u gave, but nothing works, I m getting the error "javax.xml.transform.TransformerException: Unknown nodetype: name" for 1st format, for the next 2 i got the following error "javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token".

    This is the exact format am using, XmlUtils.getAttributes(path, String.format("//*/@*",id)); here id is the string. In this I have added ur suggestions and tried,

    "//*/@*/name()",id
    "//*/@*/(name(), data(.))",id
    "//*/@*/(name(), string(.))",id

    is anything wrong in this, y I m getting exceptions, could u pls help me further.

    Thanks for your help.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath - How to get all the attribute names and values of an element

    ‏2010-03-17T09:43:08Z  
    The suggestions given by Matthias used XPath 2.0 syntax, but it seems you are using an XPath 1.0 processor.
  • SaraSmith
    SaraSmith
    3 Posts

    Re: Xpath - How to get all the attribute names and values of an element

    ‏2010-03-17T09:57:07Z  
    The suggestions given by Matthias used XPath 2.0 syntax, but it seems you are using an XPath 1.0 processor.
    Hi Michael,

    Thanks for ur reply, I am using java1.6, i think xpath1.0 is used in java 1.6, is there any other formats in 1.0, or how can I use xpath2.0 in java1.6
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Xpath - How to get all the attribute names and values of an element

    ‏2010-03-17T10:06:25Z  
    If you want an XPath 2.0 engine for Java, the obvious choice is Saxon which supports XSLT and XQuery as well.

    However, for this little problem it's not really needed.