Been using XML for ages now for data storage & transfer, but have never had to validate or transform it. Currently starting a new project and making some design decisions and need to know some rudimentary things about XSL & Schemas.
Our XML is like this (excuse the boring book example 🙂 ):
<Books>
<Book>
<ID>1</ID>
<Name>Book1</Name>
<Price>24.??</Price>
<Country>US</Country>
</Book>
<Book>
<ID>1</ID>
<Name></Name>
<Price>24.69</Price>
</Book>
</Books>
Our requirements:
-
Transformation
a) Turn “US” into United States
b) if Price > 20 create a new lLement<Expensive>True</Expensive>I’m guessing this is done with XSLT, but can anyone give me some pointers on how to achieve this?
-
Validation
a) is ID an integer, is Price a float (the most important job to be honest)
b) Are all tags filled, e.g. the name tag is not filled (2nd most important)
c) Are all tags present, e.g. Country is missing for book 2
d) [Probably tricky] Is the ID element unique through all books? (nice to have)
From what I have read this is done with a Schema or Relax NG, but can the results of the validation be outputted to a simple HTML to display a list or errors?
e.g.
Book 1: Price “Price.??” is not float
Book 2: ID is not unique, Name empty, Country missing
Or would it be better do do these things programatically in C#?
Thanks.
This stylesheet:
With your input, output:
With proper input:
Output:
Note: Ussing keys for performance. This is proof of concept. In real life, the XHTML output should be wrapped into an
xsl:messageinstruction. From http://www.w3.org/TR/xslt#messageEdit: Compacting code and addressing country map issue.
Edit 2: In real life, with big XML documents and more enterprice tools, the best approach would be to run the transformation with XSLT 2.0 schema-aware processor for validating, or run validation independly with well-know schema validators. If for some reason these choices aren’t aviable, don’t go with my proof-of-concept answer because having keys for each validation rule make cause a lot of memory use for big documents. The better way for last case, is to add rules to catch validation errors ending transformation with message. As example, this stylesheet:
With your input, this message stops the transformation:
With proper input, outputs the same as before.