Home > Blockchain >  How can I create key/keyref XPATH without namespace
How can I create key/keyref XPATH without namespace

Time:01-27

I want to create a set of key/keyref XPATH in xsd schema file without namespace.

The following code doesn't work. Key reference to an unexisting key doesn't give the error. Looks like the XPATH selector or field is not recognized.

  xmlns="http://mynamespace"

  <xsd:key name="myId">
    <xsd:selector xpath="./AAA/a"/>
    <xsd:field xpath="@id"/>
  </xsd:key>
  <xsd:keyref name="myIdref" refer="myId">
    <xsd:selector xpath="./BBB/b" />
    <xsd:field xpath="@idref"/>
  </xsd:keyref>

I also tries local-name() function like this.

<xsd:selector xpath="./*[local-name()='BBB']/*[local-name()='b']" />

Got the error:

c-general-xpath: The expression './*[local-name()='BBB']/*[local-name()='b']' is not valid with respect to the XPath subset supported by XML Schema.    

The only way I can make key/keyref work is to include namespace in each element in XPATH, like this:

  xmlns:p="http://mynamespace"

  <xsd:key name="myId">
    <xsd:selector xpath="./p:AAA/p:a"/>
    <xsd:field xpath="@id"/>
  </xsd:key>
  <xsd:keyref name="myIdref" refer="p:myId">
    <xsd:selector xpath="./p:BBB/p:b" />
    <xsd:field xpath="@idref"/>
  </xsd:keyref>

Is this the only solution? How can I create key/keyref XPATH without namespace.

CodePudding user response:

You can set e.g. <xs:schema xpathDefaultNamespace="http://mynamespace" ...>, that way your unprefixed element and type names are considered to be in that namespace. But I think this is only supported in XSD 1.1.

Additionally you can change/override your setting of that attribute on a selector or field element in the schema.

  • Related