Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XMLxsd
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 at 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 at 13-Jun-12 9:25am
   
Ok, I've reconsidered your answer, see my solution below.
barneyman at 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 at 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 at 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
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 446
1 Sergey Alexandrovich Kryukov 360
2 Shai Vashdi 318
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 9,109
1 OriginalGriff 5,290
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 13 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid