Can Schematron support parameters? - xslt

I am trying to pass in several parameters to a scheatron xlst transform, I can declare local variables and read from a file but that still means that I am either hard-coding or using a relative path to the execution location.
Does anyone know of a way to pass in a parameter, something like this is what I really need:
<xsl:param name="doctype"/>
<xsl:param name="id"/>
Any suggestion is welcome, or do I give up on schematron and go back to standard xslt?
Peter

Related

XSLT - Passing params from one template to another

I am an open-source xslt package for date format conversion.
Link # http://xsltsl.sourceforge.net/
And this is how I call in order to do my date format conversion and obtain date in the following format - April 4, 2011.
<xsl:variable name="date.format">%B %d, %Y</xsl:variable>
<xsl:variable name="someDate">
<xsl:call-template name="dt:format-date-time">
<xsl:with-param name="xsd-date-time" select="//someDate"/>
<xsl:with-param name="format" select="$date.format"/>
</xsl:call-template>
</xsl:variable>
I retrieve the result by call - <xsl:value-of select="$checkindate"/>
I would like to update my above code to pass in an additional parameter which is language and be able to get translated values with no luck.
Any help!
I would like to update my above code to pass in an additional
parameter which is language and be able to get translated values with
no luck.
The dt:format-date-time template isn't intended to accept any other parameters than it does at present.
There is more than one way to achieve the task specified above:
Modify the code of the dt:format-date-time template by adding an `xsl:param name="lang" and the corresponding code to process the new parameter.
Add a new template (say dt:format-date-time-NL). In this template have an xsl:param name="lang" and, if appropriate, call the existingdt:format-date-time` template.
I recommend the second approach as a positive example of reusing code and achieving flexibility. XSLT provides clean support for this writing style with the standard xsl:import and xsl:include directives.
Note also, that directly editing someone else's code will not always be possible, desirable or ethical. Even if alowed this can lead to multitude of incompatible "versions" (more exactly "mutations") of the original code with all bad consequences

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:calltemplate with name supplied by a parameter

I would like to call a template based on an inbound parameter to an xsl stylesheet.
Using the parameter in the name attribute fails because $ is illegal in the context. Does this mean I have to use a xsl:choose to accomplish this?
If you want to call templates selected dynamically then you can usually do it using xsl:apply-templates rather than xsl:call-template. One very general way of doing this is to change each
<xsl:template name="n">
to
<xsl:template name="n" match="xsl:template[#name='n']">
and then change your invalid
<xsl:call-template name="$x"/>
to a legitimate
<xsl:apply-templates select="document('')/*/xsl:template[#name=$x]">
And pass the context item as a parameter if necessary.
However, if we knew more about the problem you are trying to solve, we might be able to suggest a better way of solving it.
Unless you use an XSLT processor like the commercial version of Saxon 9 where you have an extension instruction like http://www.saxonica.com/documentation/extensions/instructions/call-template.xml you will need to use xsl:choose.

Dynamic XSL file

I have 3 XSL files which have paths in them to something like C:\templates\Test\file.pdf
This path isn't always going to be the same and rather than having it hard coded in the XSL, I'd like it so that the path C:\templates\test\ is replaced with a tag [BASEPATH] and when I read in the xsl file into the XSLTransform object (yes I know it's been deprecated, I may move over to the XSLCompiledTransform at the same time), I'd like the tag [BASEPATH] to be replaced with the absolute file path of the web folder (or Server.MapPath("~") seeing as it is in .net)
I thought I may be able to make an XSLLoader aspx page which takes the name of the XSL file through the querystring and then returns the XSL file via xml content-type. When I try this, I get a 503 error though so I'm not sure if you can pass urls like this into the XSLTransform.Load method.
Any ideas what to do?
Have you looked at XSL parameters?
<xsl:param name="basepath" select="'C:\Users\Graeme\'" />
<xsl:value-of select="document(concat($basepath, 'test.pdf'))" />
Then, most decent XSLT engines have a way to set a root level parameter from outside.

Varying xpath-default-namespace in XML source files

I have a set of XML files that I am processing with an XSL transform. They have a default namespace, so my XSL transform must contain the declaration:
xpath-default-namespace="urn:CZ-RVV-IS-VaV-XML-NS:data-1.2.2"
The problem is that this value changes from time to time, and my transform suddenly stops working, until I look at an example from the new file, extract this namespace ID and put it in the transform, whereby the transform stops working for old files. Is there a way to pass this as a parameter, or set it somehow at runtime? I have tried the parameter syntaxes that I looked up in various tutorials, but none have worked for this particular use.
I have searched all sorts of forums and found references to namespace-agnostic coding of XSL, but not figured out how to do it. Ian Williams' book "XSLT and Xpath" states that the default namespace must be declared, or you get nothing in the output stream, which is how it has worked for me. But I really don't want to have to change this by hand regularly, I want to give the user something that will work, without needing constant attention from me.
The only 100% reliable way I have invented so far is to use a standard programming language to open both the XML source and XSL transform as text files, extract the URI from the XML source, paste it into the XSL transform, close both files and then, finally run the actual transform. This works, but is incredibly dorky, at least to my taste. How can I better deal with changing default namespaces?
Pete
The value of xpath-default-namespace must be a static URI, so you'll have to pre-process the stylesheet if you want it to vary. One way to do that would be to use XSLT. Apply the following meta-stylesheet to your primary stylesheet each time, and then invoke the pre-processed result instead.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Pass in the new namespace URI as a stylesheet parameter -->
<xsl:param name="new-uri" required="yes"/>
<!-- By default, copy everything as is -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<!-- But update the value of #xpath-default-namespace -->
<xsl:template match="#xpath-default-namespace">
<xsl:attribute name="{name()}" namespace="{namespace-uri()}">
<xsl:value-of select="$new-uri"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
This is a bit of a strange use case though, because namespaces weren't really designed to be so dynamic. They were designed to qualify names, i.e. make up part of a name. When you look at it that way, dynamic namespaces don't make a lot of sense. Imagine a database whose table and field names arbitrarily changed every once in a while, forcing you to rewrite all your SQL scripts to keep up with the changes. That's what this is akin to.
Have you tried defining a stylesheet parameter <xsl:param name="xpdn"/> and using it in the stylesheet declaration or top level template declaration as in
<xsl:template match="...." xpath-default-namespace="$xpdn">
I can't find anything in the spec that says this won't work (but I'm not in a position to try it just now).