Aller au contenu
uencoder

Analyser XML with namespaces

XML namespaces qualify element names en avoid collisions. Apprendre le xmlns declaration, default vs prefixed namespaces, et comment en analyser them correctly.

  1. 1

    XML namespaces use URIs to disambiguate element names. A namespace is declared with xmlns="uri" (default) or xmlns:prefix="uri" (prefixed). Elements are then qualified: <prefix:element>. The namespace URI is the identifier — the prefix is just a shorthand. Two elements with the same local name but different namespaces are different elements.

  2. 2
    Formateur XML →

    Formater the XML to see namespace declarations clearly. The formatter shows which prefixes map to which URIs. Verify that all prefixed elements have a corresponding xmlns declaration in scope.

  3. 3
    Convertisseur JSON ↔ XML →

    Convertir namespaced XML to JSON. Namespace-qualified elements typically become prefixed keys: <ns:element> → "ns:element" or "{uri}element" depending on the converter. Understand your converter's namespace handling en query the correct keys

  4. 4

    Common namespaces: xmlns="http://www.w3.org/1999/xhtml" (XHTML), xmlns:svg="http://www.w3.org/2000/svg" (SVG), xmlns:atom="http://www.w3.org/2005/Atom" (Atom feeds). When parsing, match on the namespace URI, not the prefix — prefixes can be reassigned. Use XPath with namespace resolvers for robust querying.

Questions fréquentes

Why use namespaces?
To combine XML vocabularies without name collisions. For example, XHTML and SVG both have a <title> element — namespaces distinguish <xhtml:title> from <svg:title>. Without namespaces, a parser cannot tell them apart.
Is the prefix significant?
No — the namespace URI is the identifier, not the prefix. <foo:bar xmlns:foo="urn:x"> and <baz:bar xmlns:baz="urn:x"> are the same element. When comparing or querying, always use the URI, not the prefix. Prefixes are arbitrary shorthand.

Workflows résolvant cette tâche

Outils associés