Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML xs:int hexadecimal value

Tags:

xml

hex

xsd

I want to store a number in an XML file with type of or another simple integer type, but allow the user to enter the number in a hexadecimal format. Does the XML standard allow for this as I can't find anything relevant?

For example, if the XSD says:

<xs:element name="value" type="xs:integer" use="required" />

then I want the XML to be able to say:

<value>0xFF00FF</value>

or whatever the notation for hexadecimal would be.

Obviously I can try it but that only proves support in one particular implementation, not whether it's a standard. I don't particularly care whether saving to XML loses the base.

like image 956
GeoffM Avatar asked Aug 01 '12 17:08

GeoffM


Video Answer


2 Answers

I’d use a regular expression in a <xs:pattern> element.

<xs:element name="value" use="required">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="0x[0-9A-Fa-f]+|[0-9]+"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

This allows you to store both decimal and hexadecimal number (with mandatory 0x prefix). However, you’ll need to handle it specifically while converting the string to a number.

like image 189
Melebius Avatar answered Oct 25 '22 00:10

Melebius


I don't think so. xs:integer is a derived/subset type of xs:decimal, which representation is defined as

decimal has a lexical representation consisting of a finite-length sequence of decimal digits (#x30-#x39) separated by a period as a decimal indicator. An optional leading sign is allowed. If the sign is omitted, "+" is assumed. Leading and trailing zeroes are optional. If the fractional part is zero, the period and following zero(es) can be omitted. For example: -1.23, 12678967.543233, +100000.00, 210.

You could remove the 0x and set it to hexBinary, although you lose the number semantic.

like image 26
BeniBela Avatar answered Oct 24 '22 23:10

BeniBela