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.)
Related
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.
In my project, I have used the function which xaln provided. Now I need to write another function to fix the meta data of the file which is generated by . So the code just like below:
<redirect:write file="file.html">
<xsl:variable name="meta_fix" select="MetaFix:fix(string,string('file.html'))" /> //call the fix function which is an external java function.
However, what i really want is to extend the xalan redirect function and make a customize tag which will be processed just like the
<customize:write file="file.html" fixMeta="t" />
//or
<redirect:customize-write file="file.html" fixMeta="t" />
How can I extend the xalan write function and make the xalan understand my customize tag?
I believe it's possible to plug an Entity Resolver into Xalan which will be able to review and rewrite URIs, which might let you put your hint into the URI rather than a separate flag variable or attribute. Check the documentation on Apache.
Or I suppose you could try copying the logic of redirect: out of the guts of xalan and adapting apprpriately, then plugging it in as an extension element. I don't remember whether extensions would be able to access enough of xalan's guts to make that work, though, and persionally I wouldn't try it since you'd be making your stylesheets extremely nonportable.
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 ..
I am trying to truncate some text that has been formatted via HTML, but I need to keep the html in tact. I am doing so in SharePoint 2007 - so I am using XSLT 1.0.
I found this bit of XSLT here: http://symphony-cms.com/download/xslt-utilities/view/20816/
I was able to implement it, but it is telling me that the variable or parameter "Limit" has been defined twice.
However, the author has named many variables and parameters "Limit" and I am not sure which one I need to change.
I am fairly new to XSLT, and any help is greatly appreciated.
This is because at the top the XSLT the author has defined limit as a parameter
<xsl:param name="limit"/>
But a few lines down, then defines it as a variable
<xsl:variable name="limit">
Perhaps he had a 'buggy' xslt processor which allowed variables to be re-defined, but it should not actually be valid.
I did try renaming the variable to newlimit but it is hard to know whn he subsequently refers to limit whether it is the paramater or variable it is referring too (I couldn't actually get it to output useful HTML).
You are probably better off looking for something else to meet your needs. There may even be similar questions here on StackOverflow if you search about. For example, perhaps this meets your needs
XSLT - Using substring with copy-of to preserve inner HTML tags
I am sure there may be others if you look. If not, feel free to ask a new question, giving your input HTML, and your expected output, so that it is clear what your requirements are.
I'm writing a template in xslt with xpath 1.0 and need to access the anchor tag from the url of the current page. For example, the url:
http://localhost/destinations/london.aspx#accommodation
I need to be able to get #accommodation and assign it to a variable. I realise I'm somewhat limited by using xpath 1.0 - has anyone got any experience doing this?
Thanks, Adam
Why is this an xpath problem at all? A URL is not an XML document, ergo xpath does not apply.
XSLT is completely unaware of any state like page location. Guessing a bit at what you're trying to do, you're probably best off getting #accomodation from string manipulation or framework in the layer which calls the XSLT, passing the value in as a param.
OTOH maybe this is nonsensical and your question just needs clarification.
As #Annakata said, this is not an XPath problem. It doesn't seem to be an XSLT problem either, though I may be mistaken. If it is related to XSLT string parsing, then what you need is something like this question talks about.
What you probably need instead is Javascript to get the current URL (document.location) and then perform Javascript string parsing on it.
There is no way in standard XSLT to access to URL of a document : http://www.dpawson.co.uk/xsl/sect2/nono.html#d1974e804
Some vendors might provide this information via custom properties, but then you would be dependant on the XSLT processor.
If you have managed to get the URL into the XSLT in some fashion, then I suggest you will have to resort to simple string manipulation to get the anchor.