How do I reproduce Relax NG rules in C++ objects? - c++

At the momemet I am working on part of my application where I need to parse Relax NG schema and reproduce rules within file in C++ objects. I start with example code so I can explain better.
<!-- property.element -->
<define name="property.element">
<element name="property">
<attribute name="type" />
<interleave>
<zeroOrMore>
<ref name="resource.class" />
</zeroOrMore>
<ref name="literal.class" />
</interleave>
</element>
</define>
I want to create object where I can store information like:
[define] name,
element name,
attribute names,
allowed child elements with associated rules (zero or more, one or more).
Then I want to display all possible elements in my GUI where I can add only valid (in terms of Relax NG schema) elements to tree-like structure. For example - I can add only resource.class or literal.class to my property.element, every other possible element is greyed in my GUI when I have selected property.element node in GUI. I use Qt, so I load schema into QDomDocument to get access to DOM tree.
Such mechanism has to be universal i.e. no matter how elements are named, or how its structure is. In my draft I created simple class where I placed several members like: defined_name, element_name, required_attributes, optional_attributes. Currently I am stuck because I do not have any idea how to represent rules in C++ class. If I had constant set of objects on which I work I would hard-code every object, but my objects set is very likely to change drastically over time. Does anyone have any idea?

Convert the RNG file to XSD using TRANG, then convert the XSD to c++ using CodeSynthesis either XSD or XSDe). There are a bunch of samples with XSDe, so there might be one demonstrating how to validate the xml input with the schema rules.
http://www.thaiopensource.com/relaxng/trang.html
http://www.codesynthesis.com/products/xsde/

Related

Localization with xslt, do any standards exist that deal with different languages in XML?

I am working with a project where I need to create a html-file supporting several languages from an xslt-transformation. I have read several articles around this, and also looked at previous questions here in stackoverflow, like this one:
xslt localization
And the solution to put the translations in a separate xml document works just as I want it to. But I wonder if there exists any standardized/best practice for the "translate.xml" file? In the post referenced above, the following is given as an example:
<strings>
<string language="en" key="ContactDetails">Contact Details</string>
<string language="sv" key="ContactDetails">Kontaktuppgifter</string>
[...]
</strings>
As I said, the solution suggested with using keys retrieve the strings from the transalte.xml works as I want it to, but I like to use standards if they are available, so my question is if there is a standardized schema for these types of xml files, or some kind of best practice on the naming of tags etc in such a "translate.xml"?
Good question!
Yes, there is a standardized way of dealing with languages. Use the xml:lang attribute. The namespace magically exists in any XML document and is part of the core XML specification. It is defined to take a language specifier according to RFC-4646. These are the often-seen specifiers like en, en-US, es, es-BR, specifying the main language and the language variant.
The way it works is a bit like namespaces. If you define it on an element, then it is inherited by all descendants of that element, unless you redefine it, or undefine it to denote language-indepent elements.
For instance:
<text xml:lang="en">
We are
<t xml:lang="en-US">organizing</t>
<t xml:lang="en-GB">organising</t>
a conference on the effects of
<t xml:lang="en-US">color</t>
<t xml:lang="en-GB">colour</t>
in December this year.
</text>
Using a query language, i.e. XSLT, with a copy-idiom, this works excellently together with the lang() function, which takes the applicable language from the nearest ancestor or self and returns boolean true if found. It will also find the language variant like en-US if you set the main language, like en.
The following assumes XSLT 1.0, but works with 2.0 and 3.0 as well (this code was kindly corrected by Michael, see comments):
<!-- match English US language and default en -->
<xsl:template match="t[lang('en-US') or lang('en')">
<xsl:apply-templates />
</xsl:template>
<!-- remove any other <t> -->
<xsl:template match="t" />
Note: always set a default language on the outermost element, as lang() will return false if no language is found at all. You could test for this with the expression lang(''), which will return false only if no language was set at all.
About your XML files, if you have one file per language, and don't mix and match like suggested above, you can still use the same approach by setting xml:lang on the root element. Since this will then be inherited throughout the whole tree, you can still use the lang() function.

how to access nth level hierarchy of xml using only XSL

I have to extract some records from a dynamically generated xml document whose hierarchy will be of a pre-defined format but the depth of the hierarchy will not be known. I am working in a .net framework and i know that we can use .net methods from xsl and that we need to use a recursive function in this situation in conventional programming paradigm. but i dont know how to work round it using pure xsl, which is what i need to do in this particular case.
Use // before node names example:
<xsl:template match="//temp">
<!--do whatever you want-->
</xsl:template>
This will apply to all the elements that are having name temp appearing in any hierarchy ..

web service pattern that supports lazy loading of all properties

I am trying to design an endpoint template for a web service. My main requirement is that the caller is able to specify which properties should be populated in the returned result set.
My service returns large lists (up to 1M records) of partial objects as well as individual full objects such as (rough example XML, sorry it's a little verbose)
List:
<items>
<item>
<a>aaa</a>
<b>bbb</b>
</item>
<item>
<a>aaaA</a>
<b>bbbB</b>
</item>
</items>
Detail:
<item>
<a>aaa</a>
<b>bbb</b>
<c>ccc</c>
...
<w>
<x>xxx</x>
<y>yyy</y>
</w>
<z>zzz</z>
</item>
I have considered the following ideas:
Returning the full detail items in the list
Creating a 'list' item type that is shorter
passing a string array of property names that the caller wants to be returned
I am leaning towards the 3rd option but I want something different to that it doesn't support sub objects, I have considered passing the xml schema that you want returned instead of an array.
I would like the API to support lazy loading which is why the 3rd way seems viable as well.
Here's an example of what a function for 3. would look like:
public User GetUser(long ID, string[] properties)
And then the caller could just go:
User.Email = GetUser(User.ID, "Email").Email
Through extensive use of default values and hiding nulls, the returned XML for that would be:
<User>
<ID>123</ID>
<Email>example#example.com</Email>
</User>
Now the problem as mentioned above is trying to make it play nice with things like <w> far above, which itself has sub items as well as the possibility for lists to have sub items.
As I have far too many properties, I cannot have just a ws method for each property.
I am considering option 3. but using an xml schema instead of a string[].. But I can't think of an easy way to define this, I would also like to not have to use String names for properties such as "Email".
The final plan is to have a series of pre-defined schemas that are used commonly and only in advanced cases would we need to actually define the requested properties. But I have no idea of all the systems that will be talking to my API, let alone what properties they might each want (it's not going to be feasible for us to tailor the API for every caller).
Or am I over complicating everything too much?
I found the documentation for the Google APIs on Partial Responses and Partial Updates:
http://googlecode.blogspot.com/2011/07/lightning-fast-performance-tips-for.html
This seems to answer my question.

how to pass parameters to xsl file and use it to response

i have and xsl file which is rendering the question on UI.
The Question are distributed in different catagory.
Now my requirement is to pass the parameter from java code to xsl file and on the basis of that parameter i would like to perform specific operation to generate the UI.
Can Any body help me out in suggesting how to pass parameter to XSL file from JAVA code ?
Example:
/form/A/Question-Category,
/form/B/Question-Category,
/form/c/Question-Category,
/form/D/Question-Category
A,B,C,D are categories which I will pass from java code and use that token to get my XPATh of question
Say if token passed from java code is B, then expression will be '/form/B/Question-Category' .
Now my hurdle is i dont know how to pass the parameter from java code and how i can use it in XSL?
Declare the parameter like this:
<xsl:param name="category"/>
Use it like this
select="/form/*[name()=$category]/Question-Category
Then pass it from Java like this (assuming you are using the JAXP API):
transformer.setParameter("category", "a");
I don't think this is a particularly smart XML document design by the way. I think the list of categories is data rather than metadata, so I would use <category name="A"> rather than <A> to define category A. But your course tutor may have other ideas (I assume this is a student exercise, because implementing a questionnaire usually is.)

XSL include based on XSL:WHEN condition

I have a scenario here I have multiple xsl designed for different type of XML files. Now I have some application ID that is passed to my XSL library with now I want to load different xsl based on this application ID values.
Like if my application ID is 1
if application ID is 2
how can I do this???
Please help
In XSLT, xsl:include and xsl:import must be top-level elements, as said in the specifications (here for version 1.0).
That means that you can not condition the loading of another XSL file based on the XML you are applying the XSL to.