Skip to main content
XMLDir

Search 169 pages — formats, namespaces, parser messages, schema errors and tools.

XPath axe

attribute:: axis

An element's attributes, which live on their own axis and are never selected by a child step.

Attributes are not children. They sit on a separate axis, reached with attribute:: or its abbreviation @. This is why a path built entirely from element steps never returns an attribute.

@* selects every attribute of an element, which is useful for inspection.

Syntax
attribute::name — or @name
Returns
node-set
Kind
Axe
Version
XPath 1.0 (libxml2)

Worked examples

Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.

  • //line[1]/@skuon order.xmlRun
     sku="WIDGET-1"
  • //line[1]/attribute::skuon order.xmlRun
     sku="WIDGET-1"
  • count(/order/@*)on order.xmlRun
    2
  • //line[1]/@sku/text()on order.xmlRun

    (empty node-set — no matches)

    Nothing: an attribute has no text node children. Use string() or the attribute itself.

Run your own expression

What catches people out

  • Attributes have no children, so @name/text() selects nothing. The attribute node's own string-value is what you want.
  • Namespace declarations (xmlns:…) are not attributes in the XPath data model, and @* will not return them.
  • @ is an abbreviation for the axis, not an operator — @*[1] and (@*)[1] differ.

The sample documents

Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.

order.xml
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
  <customer country="NO">Ada Lovelace</customer>
  <lines>
    <line sku="WIDGET-1" qty="2"><price>14.50</price></line>
    <line sku="WIDGET-2" qty="1"><price>39.00</price></line>
    <line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
  </lines>
  <note>  Deliver after 09:00  </note>
</order>

Related

All 34 XPath entries

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.