Read properties file or a text file in xsl - xslt

i have a xsl file which is containing some contents for displaying.This contents gets changed often. so each time have to modify the xsl file.
So thought of moving the content to a text ot properties file so that just changing this will be fine.
can anybody tell me how to move just the contents to a text file and access it using xsl file.
Thanks in advance.

Why use a text file? Surely XML would be better?
An XSLT stylesheet can read a second input document using document('strings.xml'). Then you can access strings as for example
<xsl:value-of select="document('strings.xml')//string[#id='msg012']"/>
where the file has a format like
<strings>
<string id='msg012'>This is one of the strings to include</string>
</strings>
In XSLT 2.0 you can wrap the access logic into a function so the call just becomes
<xsl:value-of select="my:string('msg012')"/>

Related

Can I force the XSLT collection function to treat all files as XSL?

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'.

Inserting a new tag in existing XML file using file IO instead of parsers

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.

How to edit xml file using c++?

I have a "new_xml.xml" file as follows:
<?xml version="1.0" standalone="no"?>
<people>
<husband employed = "Yes">
<name>MArk</name>
<age>45</age>
<wife>
<wname>jenet</wname>
<age>65</age>
</wife>
</husband>
</people>
Now, i want to add following line at the top of "new_xml.xml":
<!DOCTYPE people SYSTEM "new_xmll.dtd">
So, suggest me how can i edit "new_xml.xml" using c++ and add above line into it
You can just open a new file, write the string and the Content of the old file.
ifstream oldXML("path/to/old/xml");
ofstream newXML("path/to/new/xml");
newXML<<"<!DOCTYPE people SYSTEM \"new_xmll.dtd\">"<<endl; //Write first line
newXML<<oldFile; //Copy Content of old file
You can also use a XML library if you want to Change any XML values. I like tinyxml a lot
Then probably TinyXML or MSXML would be interesting for you.
try to use cycle with eof and read each line with getline(), when you find your line just add it.

how to read an XML file that contains many "<?xml version="1.0" encoding="UTF-8"?>" with QXmlStreamReader?

After creating the xml file with QXmlStreamWriter, I am trying to add each modification of parameters at the end of the file, and I do startDocument in each addition, how can I read an XML file that contains many of
Help please please !
You will have to manually convert the document from your custom format of multiple-concatenated XML files back into real, stand-alone XML files, then pass it into the parser once it is in fact valid XML data.

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.