Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess attributes from XML in shell
    text
    copied!<p>I'm trying to parse out values from a Widget <a href="http://dev.w3.org/2006/waf/widgets/#configuration-document" rel="nofollow noreferrer">config.xml</a> using shell. I do want to use sed for this task. If there is something that <a href="http://suckless.org/" rel="nofollow noreferrer">sucks less</a> than xsltproc, I'd love to know. </p> <p>In this example I am after the <strong>id</strong> attribute value from the <code>config.xml</code> below:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;widget xmlns="http://www.w3.org/ns/widgets" id="http://example.org/exampleWidget" version="2.0 Beta" height="200" width="200"&gt; &lt;name short="123"&gt;Foo Widget&lt;/name&gt; &lt;/widget&gt; </code></pre> <p>I wish it was as simple as <a href="http://docs.jquery.com/Attributes/attr" rel="nofollow noreferrer">Jquery's attr</a>: <code>var id = $("widget").attr("id");</code></p> <p>Currently this shell code utilising <code>xsltproc</code> fails:</p> <pre><code>snag () { TMP=$(tempfile) cat &lt;&lt; EOF &gt; $TMP &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;xsl:output method="text" encoding="utf-8" indent="no"/&gt; &lt;xsl:template&gt; &lt;xsl:value-of select="$1"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; EOF echo $(xsltproc $TMP config.xml) rm -f $TMP } ID=$(snag "widget/@id") if test "$ID" = "http://example.org/exampleWidget" then echo Mission accomplished. else echo "&lt;$ID&gt; is wrong." fi </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload