3

Consider this example from w3schools:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

xmlstarlet sel -t -v "//@category" books.xml

returns only COOKING. How to get all attributes named category?

BONUS question: how do I evaluate XPath queries against an XML file without opening it in an editor nor program (I want a quick tool). The issue is that loading the huge file into memory crashes the editor. Don't tell me that eXist is my best bet?

Joe Wicentowski
  • 5,159
  • 16
  • 26
simpatico
  • 10,709
  • 20
  • 81
  • 126

2 Answers2

8

Is this what you need?

xml sel -t -m "//@category" -v "." -o " " books.xml

or to separate the results on each line

xml sel -t -m "//@category" -v "." -n books.xml

Update

You're right, your XPath was good, you just need different parameters.

cordsen
  • 1,691
  • 12
  • 10
3

Regarding why the original doesn't work: XSLT 1.0 value-of is defined to work like XPath's string() function, and string(node-set) => string(first-node). See also Feature Request: value-of multiple nodes should concat values of all nodes

npostavs
  • 4,877
  • 1
  • 24
  • 43
  • 1
    As of version 1.3.0 XMLStarlet generates code to make value-of give the value of every node separated by newlines. – npostavs Oct 10 '11 at 01:25