# How do you use \`sxpath\` to query sxml that has a default namespace?

**URL:** <https://racket.discourse.group/t/how-do-you-use-sxpath-to-query-sxml-that-has-a-default-namespace/2639>\
**Category:** Questions & Answers\
**Tags:** question\
**Created:** [January 3, 2024, 1:39am UTC](https://racket.discourse.group/t/how-do-you-use-sxpath-to-query-sxml-that-has-a-default-namespace/2639 "2024-01-03T01:39:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e9967a](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/e9967a/32/1631_2.png) [@e9967a](https://racket.discourse.group/u/e9967a)\
**Post date:** [January 3, 2024, 1:39am UTC](https://racket.discourse.group/t/how-do-you-use-sxpath-to-query-sxml-that-has-a-default-namespace/2639/1 "2024-01-03T01:39:14Z")

</div>

I am trying to use `sxpath` to query sxml that has a default namespace. For example, the atom feed for the [Racket YouTube channel](https://www.youtube.com/feeds/videos.xml?channel_id=UC8uSLYDanXDnP9Yn8UrTNzQ).

Unfortunately, the xml has a default namespace (last row).

```scheme
<feed
    xmlns:yt="http://www.youtube.com/xml/schemas/2015" 
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns="http://www.w3.org/2005/Atom">

```

...which `ssax:xml->sxml` applies to produce nodes like this:

```scheme
'(*TOP*
  (*PI* xml "version=\"1.0\" encoding=\"UTF-8\"")
  (http://www.w3.org/2005/Atom:feed
...
  (http://www.w3.org/2005/Atom:author
    (http://www.w3.org/2005/Atom:name "Racket")
    (http://www.w3.org/2005/Atom:uri
      "https://www.youtube.com/channel/UC8uSLYDanXDnP9Yn8UrTNzQ"))

```

Suppose that I am trying to retrieve the author's name. Without the default namespace, the xpath would be "/feed/author/name". However, given the namespace, it is more cumbersome

In order to use `sxpath` I need to provide the namespaces. With the named namespaces (`yt` and `media`) this is easy. However, how do I provide the default namespace?

```scheme
((sxpath "/feed/author/name" '((? . <default-url>))) sxml-string)

```

Also, thank you to everyone who contributed to the sxml library. I prefer working with it over the standard xml library, particularly because it doesn't drag in all of the whitespace pcdata from the formatting indentations and because `sxpath` allows for the use of xpath, which is much more concise than se-path.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [January 3, 2024, 9:23pm UTC](https://racket.discourse.group/t/how-do-you-use-sxpath-to-query-sxml-that-has-a-default-namespace/2639/2 "2024-01-03T21:23:38Z")

</div>

I've found default namespaces don't play well with XPath tooling in _any_ language or library. SXML is no different. For Racket, one approach I've found that works is to assign a name to the default namespace's URL when parsing the document, and then use it in queries:

```scheme
#lang racket/base

(require sxml sxml/sxpath)

(define document
  (ssax:xml->sxml
   (open-input-string #<<XML
<feed
    xmlns:yt="http://www.youtube.com/xml/schemas/2015" 
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns="http://www.w3.org/2005/Atom">
<author>
<name>John Doe</name>
</author>
</feed>
XML
                      )
   '((default . "http://www.w3.org/2005/Atom"))))

(println ((sxpath '(default:feed default:author default:name)) document)) ; '((default:name "John Doe"))

```

If you're working with stringified XPath expressions, you seem to have to include explicit mappings of namespaces as they appear in the expression to names used in the document:

```scheme
(println ((sxpath "/default:feed/default:author/default:name" '((default . "default"))) document)) 
;; but this raises an error:
(println ((sxpath "/default:feed/default:author/default:name") document))

```

(I don't think it's possible to map the default namespace to a name in `sxpath`)

---

<div class="post-metadata">

**Author:** ![e9967a](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/e9967a/32/1631_2.png) [@e9967a](https://racket.discourse.group/u/e9967a)\
**Post date:** [January 3, 2024, 11:16pm UTC](https://racket.discourse.group/t/how-do-you-use-sxpath-to-query-sxml-that-has-a-default-namespace/2639/3 "2024-01-03T23:16:21Z")

</div>

Your solution is exactly what I needed! I appreciate your time and help so much.
