{"id":7223,"date":"2017-02-15T21:12:07","date_gmt":"2017-02-15T20:12:07","guid":{"rendered":"http:\/\/www.b.shuttle.de\/hayek\/hayek\/jochen\/wp\/blog-en\/?p=7223"},"modified":"2021-09-15T17:14:03","modified_gmt":"2021-09-15T15:14:03","slug":"xmlstarlet-how-to-deal-with-a-default-namespace-in-xpath-expressions","status":"publish","type":"post","link":"https:\/\/wp.jochen.hayek.name\/blog-en\/2017\/02\/15\/xmlstarlet-how-to-deal-with-a-default-namespace-in-xpath-expressions\/","title":{"rendered":"xmlstarlet: how to deal with a default namespace in XPath expressions?"},"content":{"rendered":"<p>\t\t\t\tI ran into this problem, when I tried to extract values from JasperReport&#8217;s JRXML using xmlstarlet. JRXML files introduce a default namespace (which does not seem to serve a lot), and XPath processors need to take that into account.<\/p>\n<p>When I searched for help in xmlstarlet&#8217;s documentation and on the web, I came across this very helpful article:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.howtobuildsoftware.com\/index.php\/how-do\/bp0A\/xml-xmlstarlet-querying-value-of-xml-file-using-xmlstarlet-with-namspace\">http:\/\/www.howtobuildsoftware.com\/index.php\/how-do\/bp0A\/xml-xmlstarlet-querying-value-of-xml-file-using-xmlstarlet-with-namspace<\/a><\/li>\n<\/ul>\n<p>Once your XML has a default namespace, you have to use that default namespace with every node (that has no explicit namespace part) in your XPath expressions.<\/p>\n<p>As the article above explains, for &#8220;<em>xml sel<\/em>&#8221; each such node \u2026<\/p>\n<ul>\n<li>\u2026 either needs to make use of &#8220;_&#8221; as name of the default namespace:<\/li>\n<\/ul>\n<pre># TBD: use a true JasperReports example!\n\n$ xml sel -t -v '\/_:publications\/_:magazine' 2-023_node-sets.xml<\/pre>\n<ul>\n<li>or you assign an explicit name (let&#8217;s say &#8220;a&#8221;) to your default namespace on your xmlstarlet command line, and then you have to make use of that &#8220;explicit default namespace&#8221;:<\/li>\n<\/ul>\n<pre># TBD: use a true JasperReports example!\n\n$ xml sel -N a:'http:\/\/jasperreports.sourceforge.net\/jasperreports' -t -v '\/a:publications\/a:magazine' 2-023_node-sets.xml<\/pre>\n<p>CAVEAT: The XPath expressions created by &#8220;<em>xml el<\/em>&#8221; do not deal\u00a0with a default namespace, so you have to adapt them yourself in one of the ways explained above.<\/p>\n<p>A one-liner (e.g. in Perl) can help:<\/p>\n<pre>$ xml el FILE.xml | fgrep \/subreportExpression |\nperl -n -a -F'\/\/\/' -e 'print \"_:\",join(\"\/_:\",@F)'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I ran into this problem, when I tried to extract values from JasperReport&#8217;s JRXML using xmlstarlet. JRXML files introduce a default namespace (which does not seem to serve a lot), and XPath processors need to take that into account. When I searched for help in xmlstarlet&#8217;s documentation and on the web, I came across this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[851,732],"tags":[],"class_list":["post-7223","post","type-post","status-publish","format-standard","hentry","category-xmlstarlet","category-xpath"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paO0kP-1Sv","jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/7223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/comments?post=7223"}],"version-history":[{"count":1,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/7223\/revisions"}],"predecessor-version":[{"id":11875,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/7223\/revisions\/11875"}],"wp:attachment":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/media?parent=7223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/categories?post=7223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/tags?post=7223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}