views:

1215

answers:

3

First question: is there any way to get the name of a node's attributes?

<node attribute1="value1" attribute2="value2" />

Second question: is there a way to get attributes and values as value pairs? The situation is the following:

<node attribute1="10" attribute2="0" />

I want to get all attributes where value>0 and this way: "attribute1=10".

A: 

It depends a little bit on the context, I believe. In most cases, I expect you'd have to query "@*", enumerate over the items, and call "name()" - but it may work in some tests.

Re the edit - you can do:

@*[number(.)>0]

to find attributes matching your criteria, and:

concat(name(),'=',.)

to display the output. I don't think you can do both at once, though. What is the context here? xslt? what?

Marc Gravell
Hi Mark, thanks for the quick answer. Is there any way to get attributes and names as value pairs? Hmm... maybe I should edit my question.
Today I'm using XFactorStudio XPath 1 library and ActionScript 2.
Then I believe you're going to have to do the filter and projection separately (as above, or projection as appropriate for the environment).
Marc Gravell
It is exactly what I was trying not to do, but of course if there's no way to do anything with a query...
Well, neither name(query) nor query/name() are valid...
Marc Gravell
Marc, thanks for taking to time to try :-)
number(.) = number()
Azat Razetdinov
A: 

//node/attribute::*

Tried it using the web based XPath Evaluator here.

Use the XML you want having the sample node that you provided.
Put the XPath Expression on the page & click on Eval.

Hope this helps.

shahkalpesh
Hi, I know that wonderful site but thanks for posting the URL.
I tested this:<test><a attr1="10" attr2="0" /></test>and I got (right textarea):attr1="10"attr2="0"I'm not sure this is answering my second question...
btw, @* is the same as attribute::*
Marc Gravell
Marc: Yes, you are right. I looked at w3schools for the syntax ;)
shahkalpesh
@shahkalpesh: are you sure //node/attribute::* (or similar) I get the NAME of the attribute? I tried locally with ActionScript and I get the values, not the names
Not sure how ActionScript has XPath implemented. I tried it using the link I specified.
shahkalpesh
The following works fine: t = XPath.selectNodes(this, "test/a/@*"); trace(t[0].nodeName); trace(t[1].nodeName);
+3  A: 

First question: is there any way to get the name of a node's attributes?

<node attribute1="value1" attribute2="value2" />

Yes: This XPath expression (when node is the context (current) node)):

    name(@*[1])

produces the name of the first attribute (the ordering may be implementation - dependent)

and this XPath expression (when node is the context (current) node)):

    name(@*[2])

produces the name of the second attribute (the ordering may be implementation - dependent).

Second question: is there a way to get attributes and values as value pairs? The situation is the following:

<node attribute1="10" attribute2="0" />

I want to get all attributes where value>0 and this way: "attribute1=10".

This XPath expression (when the attribute named "attribute1" is the context (current) node)):

    concat(name(), '=', .)

produces the string:

    attribute1=value1

and this XPath expression (when the node node is the context (current) node)):

    @*[. > 0]

selects all attributes of the context node, whose value is a number, greater than 0.

In XPath 2.0 one can combine them in a single XPath expression:

    @*[number(.) > 0]/concat(name(.),'=',.)

to get (in this particular case) this result:

    attribute1=10

If you are using XPath 1.0, which is less powerful, you'll need to embed the XPath expression in a hosting language, such as XSLT. The following XSLT 1.0 thransformation :

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text"/>

  <xsl:template match="/*">
      <xsl:for-each select="@*[number(.) > 0]">
        <xsl:value-of select="concat(name(.),'=',.)"/>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<node attribute1="10" attribute2="0" />

Produces exactly the same result:

    attribute1=10

Dimitre Novatchev