Is there any possibility to use a wildcard for the document() function in XSLT like:
document("*.xml")
This is the same question: http://www.biglist.com/lists/xsl-list/archives/200108/msg00542.html
However this post is from 2001 so there might be any new techniques to solve this. Ideas?
Is there any possibility to use a wildcard for the document() function
No, there aren't any such changes to the behaviour of the document() function.
However, XPath 2.0 (and that means available in XSLT 2.0) offers the standard function collection()
Its behavior is to some extent implementation dependent.
Example (based on Saxon 9):
This XPath expression:
collection('file:///c:/?select=report_*.xml')
selects the document nodes of every XML document that resides in c:\ in a file with name starting with report_ then having a 0 or more characters, then ending with .xml.
Related
I am new to xslt and xquery . can someone please guide me with below:
I want to find out the difference in two date times is less than 24 hours by using xslt 1.0 or xquery
For example: In response I receive 2022-03-10T10:57:53.746-05:00
I want to compare it with current datetime and make sure it is less than 24 hours
To add to Martin's comment, a pure XPath 2 version (meaning compatible with XSLT 2 and all XQuery) is:
current-dateTime() - xs:dateTime('2022-03-10T10:57:53.746-05:00')
lt xs:dayTimeDuration("PT24H")
This is in case you weren't sure how to express the "less than 24 hours" condition. (An equivalent duration would be P1D.)
But this expression isn't compatible with XSLT 1.0. For that, the link to the XSLT 1.0 solution from the comments would be required.
I am trying to write two XSL files, trying to achieve following goals:
It is supposed to encrypt the input document.
It is supposed to binary encode the XML document.
Example output of 1)
<Response>
<encryptedData>e070dee5cb4688c608ee</encryptedData>
</Response>
Example output of 2)
<Response>
<compressedData>ASCDee5cb4688c608ee</compressedData>
</Response>
For functionality #1, I have a Java extension function that takes a string input and returns an encrypted string. But I don't know how to pass the input document as string to the extension function.
For functionality #2, I am not sure how to convert input to binary XML.
XSLT cannot exactly reproduce the original string representing an XML document -- due to various lexical peculiarities (and substitution of entity referencies) that are not able to reconstruct from the XmlDocument produced by the XML parser -- which is the input that an XSLT processor sees.
You can pass to the extension function the document object (/) and then the Java function can use a method like OuterXml() or InnerXml() to get one possible representation of the XML document.
I can only give an answer to your first question on how to call a java function from an XSLT.
In your stylesheet declaration you have to define a namespace, e.g. xmlns:filecounter="mappings.GenerateSequenceNumber":
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:filecounter="mappings.GenerateSequenceNumber"
exclude-result-prefixes="filecounter" version="1.0">
<xsl:output indent="yes"/>
In this case the java function is in the package "mappings" and the java class is called "GenerateSequenceNumber".
When calling the java function in your stylesheet you do for example:
<xsl:value-of select="filecounter:getSequenceNumber('countit',3)"/>
So you call the method "getSequenceNumber" in your java class and pass any variables that the java function needs in the brackets.
Unfortunately I can't help you with your second question.
I have the following string,
';#6;#'
The above string could be anything, E.g.:
';#1;#' or ';#2;#' , or ';#3;#' ...
I need to be able to replace the contents between the ' and '
Is this possible using something like translate in XSLT 1.0?
This kind of thing is quite difficult in XSLT 1.0. Take a look at the library of string-handling functions available at www.exslt.org - some of them come with XSLT implementations that you can copy into your stylesheet and call (typically as xsl:call-template).
Use substring and concat functions.
Let's say i have a full html-document as XML-input.
How would the XSLT-file look if i only want to output the first (or any) image from the html?
One XPath expression that selects the first <img> element in a document is:
(//img)[1]
Do note that a frequent mistake -- as made by #Oded in his answer is to suggest the following XPath expression -- in general it may select more than one element:
//img[1] (: WRONG !!! :)
This selects all <img> elements in the document, each one of which is the first <img> child of its parent.
Here is the exact explanation of this frequent mistake -- in the W3C XPath 1.0 Recommendation:
NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.
A further problem exists if the document has defined a default namespace, which must be the case with XHTML. XPath treats any unprefixed name as belonging to no namespace and the expression (//img)[1] selects no node, because there is no element in the document that belongs to no namespace and has name img.
In this case there are two ways to specify the wanted XPath expression:
(//x:img)[1] -- where the prefix x is associated (by the hosting language) with the specific default namespcae (in this case this is the XHTML namespace).
(//*[name()='img'])[1]
The XPath expression the will retrieve the first image from an HTML page: (//img)[1].
See the answer from #Dimitre Novatchev for more information on problems with it.
How can I include the content of a plain text file in a result document from within an XSLT 1.0 stylesheet? I.e., just like document(), but without parsing it:
<xsl:value-of select="magic-method-to-include-plaintext(#xlink_href)" />
I am almost sure, that this doesn't work without extension, because:
there is a special XPath function defined for this in XSLT/XPath 2.0:
<xsl:value-of select="unparsed-text(#xlink:href, 'UTF-8')"/>
the XSLT FAQ only lists a Java extension to achieve this via EXSLT
However, perhaps I missed something?
However, perhaps I missed something?
No, XSLT 1.0 cannot access the content of a non-xml text file without using an extension function.
One way around this is to pass the string as a global parameter to the transformation.