Example of XML namespace handling

Answered

Hello ! I'm struggling with xml dom description when it come to namespacing.

I habe a basic dom description implemented (With tags and subtags only).

I need to access values in specific namespaces, for example :    

<resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://ofbiz.apache.org/dtds/ofbiz-properties.xsd">
<property key="Accounting">
  <value xml:lang="ar">المحاسبة</value>
  <value xml:lang="cs">Účetnictví</value>
  <value xml:lang="de">Buchhaltung</value>
  <value xml:lang="zh-CN">会计</value>
  <value xml:lang="zh-TW">會計</value>
</property>
</resource>

In the above case, i'd like to be able to get the value inside the tag

<value xml:lang="zh-CN">会计</value>

How would i acces :

- the lang attribute namespace

- the value inside it ?

For now this is what i have :

import com.intellij.util.xml.*
import com.intellij.util.xmlb.annotations.Attribute

interface UiLabelFile extends DomElement {

    @SubTagList("property")
    List<Property> getProperties()

    interface Property extends DomElement {
        @NameValue
        @Attribute("key")
        GenericAttributeValue<String> getKey()

        @SubTagList("value")
        List<PropertyValue> getPropertyValues()
    }

    interface PropertyValue extends DomElement {
    }
}

I looked for examples and topics and found this one, but i don't seem to be able to find the class online or in github repo org.jetbrains.android.dom.AndroidAttributeValue

Thanks !

0
5 comments

Hi Yann, and thanks for your answer, although it's still not clear to me.
From what i understood, i have to register namespace policy with a key, but for android for axample, it links to an external resource, and i don't have such a link.
I tried the following,n but i didn't work (obviously).

class UiLabelFileDescription<S extends DomElement> extends DomFileDescription {
    private static final String rootTagName = "resource"

    public static final String XML_LANG_PREFIX = 'xml:'
    public static final String XML_LANG_NS = 'xml'

    UiLabelFileDescription() {
        super(UiLabelFile.class, rootTagName)
    }

    @Nullable
    Icon getFileIcon(int flags) {
        return PluginIcons.LABEL_FILE_ICON
    }

    @Override
    protected void initializeFileDescription() {
        registerNamespacePolicy(XML_LANG_NS, XML_LANG_PREFIX)
    }
}
interface UiLabelFile extends DomElement {

    @SubTagList("property")
    List<Property> getProperties()

    interface Property extends DomElement {
        @NameValue
        @Attribute("key")
        GenericAttributeValue<String> getKey()

        @SubTagList("value")
        List<PropertyValue> getPropertyValues()
    }

    @Namespace(UiLabelFileDescription.XML_LANG_NS)
    interface PropertyValue extends DomElement {
        @Attribute('lang')
        GenericAttributeValue<String> getLang()

        GenericDomValue<String> getText()
    }
}

The Property.getPropertyValues() returns and empty array

And my second point would be the question of the value inside the <value/> tag. How do i access it ?
The way i do it doesn't work either.

0

Hooray !! Managed to get the value of lang attr. Thanks for your help.

Now i'm still stucked with the value inside the tag, how would one get the value of the tag in such a case ? @Text doesn't work

interface UiLabelFile extends DomElement {

    @SubTagList("property")
    List<Property> getProperties()

    interface Property extends DomElement {
        @NameValue
        @Attribute("key")
        GenericAttributeValue<String> getKey()

        @SubTagList("value")
        List<PropertyValue> getPropertyValues()
    }

    interface PropertyValue extends DomElement {
        @Attribute('lang')
        XmlOfbizAttrValue<String> getLang()

        @Text
        GenericDomValue<String> getText()
    }

    @Namespace(UiLabelFileDescription.XML_LANG_NS_NAME)
    interface XmlOfbizAttrValue<T> extends GenericAttributeValue<T> {}
}
0

Hi yann, found it about an hour before your answer, and didn't get time to answer after this.

I'm all set now, thank you for your help and patience

0

Please sign in to leave a comment.