Click here to Skip to main content
12,397,805 members (54,538 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: XML xsd
Hello!

I want to compose a simple type within a xsd schema.
Here a xml example:
<Unit Name="milimeter" Abbr="mm" Definition="0.001 m" />
The 'Definition' attribute consists of two parts: a xsd:double ("0.001") and a custom type (UnitAbbreviation, "m", used for the 'Abbr' attribute too).
So simple types for both parts exists, but how can I join them without writing a regex which redefine both the xsd:double and UnitAbbreviation?

I look for something like this:
<xs:simpleType name="UnitDefinition">
  <xs:composedType type="xsd:Double" />
  <AndHereSomeKindOfWhiteSpace />
  <xs:composedType type="UnitAbbreviation"/>
</xs:simpleType>
I am aware of the snippet beeing pseudo code...

Is this able with XSD types? Or do I have to write a big regex restriction, which covers both the xsd:double and the UnitAbbreviation? (the xsd:double should allow scientific notation...)
Thanks for replying,
Henning
Posted 12-Jun-12 7:55am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You'll have to build a huge regex ... or solve it in an alternative fashion ..

How about having child nodes that define the Unit, that way you can have multiple definitions, the XSD is easier to construct and you get easier lookups?

ie.

<Unit Name="milimeter" Abbr="mm">
  <Definition Parent="metre" UnitConversion="0.001"/>
  <Definition Parent="inch" UnitConversion="0.03937"/>
</Unit>

I've got the parent/conversion logic a bit screwed there, but you get the point Smile | :)
  Permalink  
Comments
Henning Dieterichs 13-Jun-12 3:28am
   
Hi, thank you very much for your answer!
The idea sounds good, but I think it gets to complicated when complex units are defined (e.g. km/h or mph), which have multiple parents...
And complex types can have children, too (cubicliter, e.g.).

Greetings,
Henning
Henning Dieterichs 13-Jun-12 9:25am
   
Ok, I've reconsidered your answer, see my solution below.
barneyman 13-Jun-12 9:34am
   
I'm glad I could aid your creative process ... As an aside, I intend to implement something similar in the next rev of our product, if only so I can squeeze in 'cubits' ;)
Henning Dieterichs 13-Jun-12 9:54am
   
I will publish my implementation soon here on codeproject.
The system can convert (only by knowing the basic units and their factors) from km/h to mph or meters per seconds, or from centiliter to cubicmeter (I never got it right in math education ;))
My units of measure library will have another (currently top secret) feature, which is depending on this question (http://www.codeproject.com/Questions/403187/MSBuild-Resolve-Project-Reference-to-Assembly), maybe you can imagine what it will be.
Henning Dieterichs 26-Jun-12 11:58am
   
Now it is published:
http://www.codeproject.com/Articles/404573/Units-of-Measure-Library-for-NET
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Mapping the n:n unit-hierarchy to the 1:n relationship of parent and children in xml will add more complexity (though it is in the sense of xml), since the format won't be extendable (you can simply add a new element to the bottom of a list, but in an hierarchy you have to specify where to add it) and composed units like speed have no real parents.

Defining units by an expression like definition="km/h" sounds more natural, but seems not to be in the sense of xml (definition within a string vs. definition through a semantic xml node).

So I combined my first solution and the solution of barneyman:
<?xml version="1.0" encoding="utf-8" ?>
<UnitLibrary xmlns="http://www.hediet.de/xsd/unitlibrary/1.0">
  <BaseUnit Name="Meter" Abbr="m" />
 
  <SimpleUnit Name="Kilometer" Abbr="km" Factor="1000" UnderlayingUnit="m" />
  <SimpleUnit Name="Milimeter" Abbr="mm" Factor="0.001" UnderlayingUnit="m" />
 
  <BaseUnit Name="Second" Abbr="s" />
 
  <SimpleUnit Name="Minute" Abbr="min" Factor="60" UnderlayingUnit="s" />
  <SimpleUnit Name="Hour" Abbr="h" Factor="60" UnderlayingUnit="min" />
 
  <ComplexUnit Name="Kilometers per Hour" Abbr="kmh">
    <UnitPart Power="1" Unit="km" />
    <UnitPart Power="-1" Unit="h" />
  </ComplexUnit>
  <ComplexUnit Name="Meters per Second" Abbr="mps">
    <UnitPart Power="1" Unit="m" />
    <UnitPart Power="-1" Unit="s" />
  </ComplexUnit>
</UnitLibrary>

I think this will be the best.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 13 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100