XMLStarlet Namespaces definition
Asked Answered
C

2

5

I need your help on the Namespaces for XMLStarlet. (never saw a library that badly explained) I have an XML file like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx version="2005-1" xml:lang="fr" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head>
<meta name="dtb:uid" content="9782721213747"/>
<meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
</head>
<docTitle>
<text>My text</text>
</docTitle>
<navMap>
<navPoint id="NavPoint-1" playOrder="1"><navLabel><text>Couverture</text></navLabel><content src="01_cover.html"/></navPoint>

And so on.

So now, my queries in XMLstarlet fail due to the presence of the namespaces.

xmlns="http://www.daisy.org/z3986/2005/ncx/"

I have read that you can use SED to remove the namespaces. Just like that:

cat Myfile.ncx | sed -e 's/ xmlns.*=".*"//g' | xmlstarlet ed -d "/ncx/navMap/navPoint[@playOrder='5']"

Which works just fine. Problem is, I need the namespaces and do not want to remove them.

Also, I have tried this:

xmlstarlet -N x="http://www.daisy.org/z3986/2005/ncx/" ed -d "/ncx/navMap/navPoint[@playOrder='5']" Myfile.ncx

which does not work. (also not understanding what means x in x="http..." I read some put "my" some put other values ... and there is no man page for this in the XMLStarlet pages)

Any idea how to use the XMLStarlet with this XML file and KEEP the namespaces in the output?

(I use XML starlet to remove some nodes I do not use. It's way safer to make it this way rather than using shell commands)

Huge thanks to whoever helps ;)

Clydesdale answered 20/6, 2013 at 11:46 Comment(0)
K
4

not understanding what means x in x="http..." I read some put "my" some put other values

The x is the name you are giving to the namespace. You then have to use it in the query:

xmlstarlet ed -N x="http://www.daisy.org/z3986/2005/ncx/" -d "/x:ncx/x:navMap/x:navPoint[@playOrder='5']" Myfile.ncx

The -N option must follow the ed command, as @reinierpost noted.

... and there is no man page for this in the XMLStarlet pages

The man page is rather lacking, there is an explanation in chapter 5 of the user's guide.

Kozloski answered 22/6, 2013 at 13:36 Comment(0)
B
4

Starting with version 1.2.1 you can use de default namespace _
(drop the -N argument)

xmlstarlet ed -d "/_:ncx/_:navMap/_:navPoint[@playOrder='5']" Myfile.ncx

As explained here:

1.3. A More Convenient Solution

XML documents can also use different namespace prefixes, on any element in the document. In order to handle namespaces with greater ease, XMLStarlet (versions 1.2.1+) will use the namespace prefixes declared on the root element of the input document. The default namespace will be bound to the prefixes "_" and "DEFAULT" (in versions 1.5.0+).

Beguin answered 28/6, 2017 at 18:6 Comment(1)
This is what generated the advent of JSON.. Just had to add underscore prefixes to my XPath elements, at least for the document I was using, in order to select anything!Headless

© 2022 - 2024 — McMap. All rights reserved.