Xml namespaces



  • TR:wtf: is obviously me for not understanding the thing, but I am clearly not alone…

    Our company just loves XML. So I needed to add some commands to one protocol, which means adding a schema for it. The schema goes along the lines of:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns="http://company.com/Something"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://company.com/Something"
        elementFromDefault="unqualified"
        id="Something">
        <xs:element name="Something">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Parameter" type="xs:string"
                        minOccurs="0" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    and the document has namespace declared with a prefix like this:

    <?xml version="1.0" encoding="utf-8"?>
    <n:Something xmlns:n="http://company.com/Something">
        <Parameter>Foo</Parameter>
        <Parameter>Bar</Parameter>
        <Parameter>Baz</Parameter>
    </n:Something>
    

    Prefixes are used like this in several communication channels. Except one. There the messages instead look like:

    <?xml version="1.0" encoding="utf-8"?>
    <Something xmlns="http://company.com/Something">
        <Parameter>Foo</Parameter>
        <Parameter>Bar</Parameter>
        <Parameter>Baz</Parameter>
    </Something>
    

    So when it is there, I thought I'll write it without the prefix too. Well, no. Validator says:

    Element '{http://company.com/Something}Parameter': This element is not expected. Expected is ( Parameter ).
    

    So I look in the schema for the one special :snowflake: service and it looks like:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns="http://company.com/Something"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://company.com/Something"
        elementFromDefault="unqualified"
        id="Something">
        <xs:element name="Something">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="Parameter"
                        minOccurs="0" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="Parameter" type="xs:string"/>
    </xs:schema>
    

    And of course the first document with the last schema fails too, this time with:

    Element 'Parameter': This element is not expected. Expected is ( {http://company.com/Something}Parameter ).
    

    Obviously the elements declared on top-level are in the namespace while elements declared inside something are not. Ok.

    What I don't get is:

    1. Why attributes behave differently (they are used without prefix in both versions)?
    2. Why are the rules so complicated that nobody really understand them?
    3. And what what whoever designed this smoking?
    4. And a bonus question, what was whoever designed Android manifests smoking? That must have been even better stuff (Android manifests have all attributes in a namespace, but none of the elements).

    PS: If somebody could explain to me how to use this in a sane, consistent manner, I would be genuinely glad; so far I've seen many different styles without any apparent logic to them.

    PPS: Using the same schema URI in both is not result of anonymisation. The application does have several protocols that all use the same schema URI, but completely different definitions. The use of the same elements is though; the schemata are really completely different.



  • In this snippet

    <?xml version="1.0" encoding="utf-8"?>
    <n:Something xmlns:n="http://company.com/Something">
        <Parameter>Foo</Parameter>
        <Parameter>Bar</Parameter>
        <Parameter>Baz</Parameter>
    </n:Something>
    

    the namespace only applies to Something as the prefix matches; the Parameters are unprefixed, so are in the default (root) namespace.

    However, in this snippet

    <?xml version="1.0" encoding="utf-8"?>
    <Something xmlns="http://company.com/Something">
        <Parameter>Foo</Parameter>
        <Parameter>Bar</Parameter>
        <Parameter>Baz</Parameter>
    </Something>
    

    the default namespace changes with Something, and this cascades down to the Parameters.

    In other words, Parameter is in the root namespace in snippet 1, but in the http://company.com/Something namespace in snippet 2.

    And this is also why people now use JSON.


  • Discourse touched me in a no-no place

    @Bulb said in Xml namespaces:

    Why attributes behave differently (they are used without prefix in both versions)?

    Because for some reason, attributes are not in a namespace (other than the namespace with the empty name URL) unless you explicitly say they are. (It's possible to override the behaviour, but that causes more confusion.)

    Why are the rules so complicated that nobody really understand them?
    And what what whoever designed this smoking?
    And a bonus question, what was whoever designed Android manifests smoking?

    :shrug:



  • @RaceProUK Yeah, I understood that much. What I do not understand is why there are so many options and which ones to use to keep at least some sanity.

    (edit: +1 for “this is also why people now use JSON.”)



  • @Bulb said in Xml namespaces:

    to keep at least some sanity

    This is XML.

    Nobody expect you to keep some sanity :wink:



  • @Bulb said in Xml namespaces:

    Yeah, I understood that much. What I do not understand is why there are so many options and which ones to use to keep at least some sanity.

    Ah. In that case, I'll actually answer the questions you asked :D

    @Bulb said in Xml namespaces:

    Why attributes behave differently (they are used without prefix in both versions)?

    I never really understood this myself. And @dkf has answered this already.

    @Bulb said in Xml namespaces:

    Why are the rules so complicated that nobody really understand them?

    These are nothing compared to the CSS Box Model and selector precedence rules.

    @Bulb said in Xml namespaces:

    And what what whoever designed this smoking?

    Something far weaker than what the people who designed the aforementioned CSS rules were smoking.

    @Bulb said in Xml namespaces:

    And a bonus question, what was whoever designed Android manifests smoking? That must have been even better stuff (Android manifests have all attributes in a namespace, but none of the elements).

    I'd like to know that too, so I can avoid it at all costs.



  • @Bulb

    1. Why attributes behave differently (they are used without prefix in both versions)?
    <xs:schema xmlns="http://company.com/Something"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://company.com/Something"
        elementFromDefault="unqualified"
        id="Something">
    

    Note that elementFromDefault attribute: it means that any element declared under another element is "local" and uses a different namespace. Attributes also use "unqualified" by default but they are always defined under some element, hence they're local and use a different namespace.

    Change that elementFromDefault to "qualified" and suddenly all elements will require a namespace, either through the non-prefixed namespace or through an explicit prefix.

    It does change the meaning of your XMLs though - all previous XMLs made for the first XSD will now be expected to look like XMLs fro the second XSD!

    1. Why are the rules so complicated that nobody really understand them?

    When XML was designed, namespaces were allegedly quickly rushed in. It's more fun for the people building XML DOM APIs because nobody ever though of how inserted elements should behave.

    As for weird behaviour in XML schema: top-level elements really mean something different than nested elements. The second XSD will actually allow an XML like this:

    <?xml version="1.0" encoding="utf-8"?>
    <Parameter>Foo</Parameter>
    

    Namespaces are even more fun in XPath though because there's no real syntax to select an element in a given namespace, you need to rely on the XPath evaluator supporting prefixes and somehow declare those prefixes.

    1. And what whas whoever designed this smoking?

    I don't know, but they fancied another form and didn't know Xml Schema.

    1. And a bonus question, what was whoever designed Android manifests smoking? That must have been even better stuff (Android manifests have all attributes in a namespace, but none of the elements).

    No idea as I don't work with Android, and from the sound of it I don't want to know.



  • @RaceProUK said in Xml namespaces:

    These are nothing compared to the CSS Box Model and selector precedence rules.

    I think I mostly managed to get the box model. And I even think I understand why that is so messy: basically the original rules (for display: inline and display: block) are severally lacking, so display: flex was introduced, but since it is new and many people still didn't heard of it, most pages contain ugly hacks instead.

    Site note: yeah, we have a lot of that in our app (hybrid mobile) too. Just last month a colleague left for vacation and not an hour later another colleague reported the thing he just wrote drew partially off-screen due to sizing hacks. So I had to find out (I had no experience with HTML5; he should have) about display: flex and rewrite half of the style-sheet.

    The selector precedence rules, yeah, I don't get those either.

    @JBert said in Xml namespaces:

    Note that elementFromDefault attribute

    Started suspecting that. Now I wonder what whoever put that in was smoking.

    @JBert said in Xml namespaces:

    When XML was designed, namespaces were allegedly quickly rushed in.

    So business as usual…

    @JBert said in Xml namespaces:

    how inserted elements should behave.

    The hand-rolled serializer that builds the above using libxml2 (that's another story for long winter evenings—libxml2 is :poop:) built it without namespaces and had a hacky utility method something like fixupDocumentNamespace that injected the namespace in the root node (not the document, because the child elements shouldn't get it). Colleague recently rewrote it to proper C++ wrapper to at least stop all the leaks, but it's obviously still a hand-rolled serializer.

    @JBert said in Xml namespaces:

    The second XSD will actually allow an XML like this

    Yeah, I realised.

    @JBert said in Xml namespaces:

    I don't know, but they fancied another form and didn't know Xml Schema.

    Well, of course they did not. XMLSchema was designed long after XML itself. (Remember those crappy DTDs?)



  • @Bulb said in Xml namespaces:

    Note that elementFromDefault attribute

    Started suspecting that. Now I wonder what whoever put that in was smoking.

    … but of course I suspect I know. They simply didn't find how to set the namespace for all elements of the document in libxml2, just how to add it to specific element. So they added it to the root only.

    And it's not hard to miss something in libxml2—its documentation is the kind which describes every function, but says exactly nothing useful (it just repeats name of each function, in more grammatical form, as the, only, comment for it).


  • mod

    @Bulb said in Xml namespaces:

    , so display: flex was introduced,

    That's flexbox. Box model is older: https://www.w3.org/TR/CSS2/box.html



  • While XML has its inconveniences, being able to pull out things based on a namespace is seriously useful sometimes.



  • This post is deleted!


  • @Yamikuronue It took me a little while to get my head around box model but the only thing I really found unintuitive was negative margins/padding. Perhaps there's some additional subtleties I'm missing.

    I'll grant you there's always the question of "margin on the child or padding on the parent" when you trying to decipher or create something with a lot of nesting, but decent browser dev tools go a long way towards making that easier.



  • @Yamikuronue Well, flexbox is not outside and beyond box model. It was added into it.

    Before that, box model was inadequate, because

    Roses are red,
    Violets are blue;
    Rhymes can be typeset,
    With boxes and glue.¹
    But without glue,
    nothing will do.

    and there were simply no fillers for distributing the remaining white space.



  • @Bulb It's also not helped by the fact that when the box model was introduced, MS implemented it differently to everyone else. And that was when IE6 was the main browser. Of course, all browsers (IE included) now use the standard box model instead of the wrong IE6 version… until CSS3, where you can choose which one to use.



  • @RaceProUK said in Xml namespaces:

    CSS3

    Yeah, the “living standard” bullshit. After all, it's all the same W3. Their specifications were always crap and still are. All of them. Convoluted, slapped together haphazardly and pretty hard to read. I've looked things up in specifications of programming languages, protocols and stuff and the W3 ones are consistently hardest to use.

    …. reminds me, I think our application has, among other cruft, box-sizing: border-box all over the place, but I am not sure and I am already home for today and can't check. Yeah, nobody uses padding: there anyway. Might be a workaround for the padding:s in the Bootstrap stylesheet we inherit because something didn't work as the original coder intended (it is not a browser workaround, because it is a mobile app and both Android and iOS use WebKit).


  • Discourse touched me in a no-no place

    @JBert said in Xml namespaces:

    When XML was designed, namespaces were allegedly quickly rushed in.

    There were a few years between. The really crazy bits about XML namespaces are that you name them with URIs (:wtf:) despite URIs having no sane equivalence rules nor there being any rules about resolving said URIs to anything, and that those are so unwieldy that they have a short name too, but the short names can be anything the user picks (:wtf:²) and you can put the names inside elements and attributes and have them understood that way too (:wtf:³).

    But that's as nothing by comparison with the lunacy in XPath, or the sheer mind-blowing asinine stupidity inflicted on the world with DOM…

    OTOH, XML Schema is actually less crazy than a DTD. More useful too.

    It's more fun for the people building XML DOM APIs because nobody ever though of how inserted elements should behave.

    I don't know what you're talking about here? XML documents never had a behaviour model. XHTML might do, but general XML doesn't. It just describes a DOM tree; nothing more.



  • @dkf said in Xml namespaces:

    but the short names can be anything the user picks

    … except half the programs out there don't actually bother checking the URI and expect specific prefix.

    @dkf said in Xml namespaces:

    lunacy in XPath

    The set of operators is mostly sensible. The syntax ranges from weird to ass-backwards. And the set of functions is just random, like in all W3 standards.

    @dkf said in Xml namespaces:

    XML Schema is actually less crazy than a DTD.

    Yes. XML Schema is just verbose and obtuse while DTD was outright insane (and mostly useless indeed).

    @dkf said in Xml namespaces:

    It's more fun for the people building XML DOM APIs because nobody ever though of how inserted elements should behave.

    I don't know what you're talking about here? XML documents never had a behaviour model.

    XML documents don't, but the DOM operations for adding nodes should.


  • Discourse touched me in a no-no place

    @Bulb said in Xml namespaces:

    The set of operators is mostly sensible.

    Until you get into the whole business of using the different axes, when the funny juice starts to be more apparent.


  • Winner of the 2016 Presidential Election

    @dkf said in Xml namespaces:

    The really crazy bits about XML namespaces are that you name them with URIs (:wtf:)

    @dkf said in Xml namespaces:

    those namespace URIs are so unwieldy that they have a short name too, but the short names can be anything the user picks (:wtf:2)

    These two make some sense to me. The first basically acts like a guid via the proxy of the DNS system, and the second is used even in languages with nicer namespacing:

    using Co = Company.Proj.Nested;  // define an alias to represent a namespace
    

    @dkf said in Xml namespaces:

    you can put the names inside elements and attributes and have them understood that way too (:wtf:³)

    And for some reason this makes sense to me as well, because it's an attribute modifying the interpretation of the objectelement.



  • @Bulb said in Xml namespaces:

    box-sizing: border-box all over the place

    Yes, all over the place. But it is actually not a WTF at all, because that is the only way to size several boxes to the size of the parent and still have some padding in them. Because CSS can't specify with of 10% - 20px.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.