I have to pass multiple input xml files and generate multiple xml files as output.
If 'm having 3 files as 'File1.xml', 'File2.xml' and 'File3.xml',
I need to copy the full content of those xml's and also add a node to each xml and then display the output files as 'File1_op.xml', 'File2_op.xml' and 'File3_op.xml'. The output files will have a extra node added like
<Product>45896</Product>
I can't pass the file names as static, because it may change. So I need to pass the directory itself and process all the xml files in that particular directory.
Is this possible in xslt? Any help please.
Using Saxon XSLT 2.0 you're on the bright side - I don't have this on hand so I can't give detailled help. My suggestions are
for input file names Running a single stylesheet on multiple input documents and placing all their outputs in a single file
for output Why does Saxon evaluate the result-document URI to be the same?
Related
I'm currently building an XSLT stylesheet used to document other XSLT stylesheets in a series of folders and sub-folders. My code pulls out specific details about variables, functions, etc and renders it in an output format. The sheets being read are created by a 3rd party product. Most of them have an XSL extension but some of them are proprietary extensions. I have some files with a DTCBS extension but they are just XSL stylesheets.
I'm currently loading the content of these files into a variable using the XSLT function "collection" as follows:
<xsl:variable name="Collection" select="collection(concat('file:///', encode-for-uri(replace($filePath, '\\', '/')),'?select=*.(xsl|dtcbs|xml);recurse=yes'))" as="node()*"/>
The variable works just fine if I use XSL|XML. But if I include the DTCBS extension, the variable blows up citing "the supplied value is xs:base64Binary".
If I manually put the xml declaration line at the top of my DTCBS file, the variable works fine. Those DTCBS files are auto-generated without the declaration line so I can't fix that, nor can I manually edit them each time I want to run my documenter code.
From what I can tell, because it's not an XSL extension, and the XML declaration line isn't present, the XSLT parser thinks it's base64 when it isn't.
I'm using Saxon as my XSLT parser and the Saxon documentation says it uses file extensions and http headers to detect the file type.
Does anyone know if there is a way to force collection() to treat every file as an XSL?
Tried adding the XML declaration line in the DTCBS file. This did correct the issue but I can't do this in all cases as I am trying to automate the entire thing.
I also renamed the DTCBS extension to XSL and the problem went away as well.
As well as Martin's suggestion, you can register content types with the Saxon configuration:
processor.getUnderlyingConfiguration()
.registerFileExtension("dtcbs", "application/xml");
This has been available since Saxon 9.7.
Try to add e.g. content-type=application/xml e.g. '?select=*.(xsl|dtcbs|xml);recurse=yes;content-type=application/xml'.
I have an existing custom xml file for which I need to add a tag basically append it to the end of root node. But as the xml format will not be changed it can be safely assumed that parsing is not required and only need to add this tag in the end of the file.
XML format:
<links>
...
...
<url type="search">www.google.com</url>
</links>
In the above xml file, I would like to add the tag.
File operation suggests we cannot add in the middle of the file. Can anyone suggest other approaches?
"Safely" is always relative. You can never "safely" assume that you can process XML without parsing unless you are 100% in control of the process that generates the XML, and are confident you will remain so.
And if you are in control of the XML, then I would suggest maintaining it as two files: a content.xml file that holds everything except the root element, and a wrapper.xml file that references content.xml as an external entity:
<!DOCTYPE links [
<!ENTITY e SYSTEM "content.xml">
]>
<links>&e;</links>
and then you can use a normal file append operation on content.xml to add to the content.
I want to run several Xslts with one cached xml . However I can only find ways to run several xml files with
a cachedxslt , but thats not what I want . As well as an XsltExecutable for Xslt , can there be an XsltExecutbale for Xmls, so that i can cache an xml , and run several xslts on it.
You can create a DocumentBuilder and use parseXmlFromFile (https://www.saxonica.com/saxon-c/doc11/html/classDocumentBuilder.html#a4fd6acc79cbed4ae3aa9bd062ffa080f) to parse your XML input file once into an XdmNode, then you can feed that XdmNode as the input to any transformTo... method you call on your XsltExecutable or run any applyTemplates.. method after setting that XdmNode as the initial match selection with the method setInitialMatchSelection.
For example,I have this folder by name, test under which some n number of images are stored with .jpg extension as well as some log files. Now I need top get only .jpg images but not log files using XSLT. Is this possible.?
In Saxon 9.7 with XSLT 3.0 support you can get a sequence of URIs with a certain file name ending with e.g. uri-collection('directory-name?select=*.jpg'). I am not sure what you want to do exactly when you say "get all the images", the suggestion would allow you to get the file URIs of the images, e.g. file:/root/directory-name/file1.jpg.
I'm working part of a project that needs to display a .xml file with .xsl files.
Here's part of the code:
FILE_PATH = "myxsl.xsl"
...
Label content = new Label("content",xmlContent);
content.setEscapeModelStrings(false);
content.add(new XsltTransformerBehavior(FILE_PATH));
add(content);
...
Currently the page works if I use only one .xsl file. However, because the .xsl files I need to deal with can be really long, they are separated into several components.
for example, I will have
mymain.xsl, head.xsl, tables.xsl
the mymain.xsl has inclusion of the other like this
<xsl:include href="head.xsl"/>
<xsl:include href="tables.xsl"/>
I tried to set FILE_PATH to mymain.xsl, but it didn't work. The program can find mymain.xsl but cannot compile the stylesheet because it cannot find head.xsl and tables.xsl
I've been searching for a long time but still have no clue how to do this. Really appreciate any help. Thanks in advance.
I looked at the documentation of XsltTransformerBehavior, and it looks like it doesn't expose the functionality to set a custom URI resolver (which is basically what you want to do).
However, in the case that no-one else has a better solution, I can propose a workaround: Write an XSLT that merges your XSLT files into one file. See here for an XSLT that does this. If you're using Maven as build system, you can do this automatically in the generate-resources phase using the transform goal of the Maven XML Plugin. You can then associate the XsltTransformerBehavior with the single-file XSLT which will be available at run-time.