Get XSLT result as text and use in another XSLT - xslt

I have a requirement to use one XSLT result as text value for an element in another XSLT.
Example: My output XML should be like
<?xml version="1.0" encoding="utf-8"?>
<root>
<a>xxx</a>
<b>yyy</b>
<c>
<?xml version="1.0" encoding="utf-8"?><order><header>....</order>
</c>
</root>
"Order.xsl" is used to get XML.
"Main.xsl" is used to get XML which has to call "Order.xsl" and get the result as text for element.
Please can you give your suggestions?
Thanks

Your output "XML" is not actually well-formed XML so you can't produce it using XSLT. It wouldn't be very useful if you could, because no XML parser would accept it. An XML declaration is permitted only at the start of the XML document.

Related

Xpath Matching a node and getting the value of it

Below is the xml file:
file1.xml
<?xml version="1.0" encoding="UTF-8"?><W4N xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:functx="http://www.functx.com"><LUNGROUP><OBJECT lungroupID="0" lunIds="0,221,228"/></LUNGROUP><LUNGROUP><OBJECT lungroupID="1" lunIds="1,3,5/></LUNGROUP></W4N>
I want to match on lunIds. I have given the below xpath expression /W4N/LUNGROUP/OBJECT[tokenize(#lunIds,',')='228']
Its showing the result as Elements found: 1
Now my requirement is to get the lungroupID of the matched element.How can I do this using xpath? Any help is highly appreciated.
I don't see the XML you intended to post, but you should be able to add the attribute you want at the end of your xpath expression:
/W4N/LUNGROUP/OBJECT[tokenize(#lunIds,',')='228']/#lungroupID

Get file name where the content is present in

I've the below XML.
<?xml version="1.0" encoding="UTF-8"?>
<entry>
<file name="FILE_ORD_01.xml"/>
<file name="FILE_ORD_02.xml"/>
<file name="FILE_ORD_03.xml"/>
<file name="FILE_ORD_04.xml"/>
<file name="FILE_ORD_05.xml"/>
</entry>
Basically this is a list of files in my folder
In every XML file there is a phrase.
and I've another XML file from where i need to get the phrase values compare them with the phrase values in this list and give in which list is this phrase present in.
<xsl:variable name="prent">
<xsl:for-each select="document('C:\Users\u0138039\Desktop\Proview\MY\2015\title.xml')/entry/file">
<xsl:value-of select="normalize-space(document(concat('C:\Users\u0138039\Desktop\Proview\MY\2015\',./#name))/chapter[//page/#num=regex-group(1)])"/>
</xsl:for-each>
</xsl:variable>
using this code i'm able to see in which file is the phrase match found in for example if the match is found in FILE_ORD_03.xml i want to print FILE_ORD_03
Here basically i want to get the file name in which the phrase is present in from the above list and print its value. by using base-uri() or getting the attribute value directly
Thanks
I don't think you can extract the value from your variable as there you have simply text nodes with the normalized string value, but I think you want something along the lines of
<xsl:variable name="file-names" as="xs:string*"
select="document(document('file:///C:/Users/u0138039/Desktop/Proview/MY/2015/title.xml')/entry/file/#name)/chapter[//page/#name = regex-group(1)]/substring-before(tokenize(document-uri(/), '/')[last()], '.')"/>
to extract the file name(s) of matching file(s) as a sequence of strings into a second variable.

How to avoid Open xml tag on its own line

I have an XML structure as below:
<?xml version="1.0" encoding="utf-8"?>
<cl:doc identifier="ISBN" xsi:schemaLocation="http://xml.cengage-learning.com/cendoc-core cendoc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cl="http://xml.cengage-learning.com/cendoc-core" xmlns:m="http://www.w3.org/1998/Math/MathML">
<cl:chapter identifier="ch01">
<cl:opener identifier="ch06_opn">
<cl:introduction identifier="ch06_int">
<cl:list identifier="tu_1" list-style="Unformatted" item-length="long">
<cl:item identifier="tu_2"><cl:para identifier="ch01_dum_2">Solubility</cl:para></cl:item>
<cl:item identifier="tu_3"><cl:para identifier="ch01_dum_3">Polarity</cl:para></cl:item>
</cl:list></cl:introduction></cl:opener></cl:chapter></cl:doc>
When I transform this above xml using XSLT, I got the below output:
<?xml version="1.0" encoding="utf-8"?>
<cl:doc identifier="ISBN" xsi:schemaLocation="http://xml.cengage-learning.com/cendoc-core cendoc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cl="http://xml.cengage-learning.com/cendoc-core" xmlns:m="http://www.w3.org/1998/Math/MathML"><cl:chapter identifier="ch01">
<cl:opener identifier="ch06_opn">
<cl:introduction identifier="ch06_int"><cl:list identifier="tu_1" list-style="Unformatted" item-length="long">
<cl:item identifier="tu_2"><cl:para identifier="ch01_dum_2">Solubility</cl:para></cl:item>
<cl:item identifier="tu_3"><cl:para identifier="ch01_dum_3">Polarity</cl:para></cl:item></cl:list></cl:introduction></cl:opener></cl:chapter></cl:doc>
Here, the opening tag <cl:opener identifier="ch06_opn"> alone comes on separate line. This result me to have the blank line after doing the conversion.
I need this <cl:opener identifier="ch06_opn"> tag must be run-on with either its previous line or to the next line.
Can anybody help me how this can be achieved through XSLT.
Thanks,
Gopal
Without seeing your XSLT it's difficult to be certain, but it sounds like your XSLT is copying over the whitespace in the source into the output.
The quickest way to prevent that is to put
<xsl:strip-space elements="*"/>
or alternatively
<xsl:template match="text()[not(normalize-space())]"/>
This removes all whitespace, but you can of course be more specific about the whitespace you're removing, such as
<xsl:template match="cl:opener/text()[1][not(normalize-space())]"/>
to remove just the whitespace after that opening element tag- this matches the first text node within cl:opener if it's whitespace only, and outputs nothing in it's place.

why does xslt output encoding=utf-8 not convert iso-8859-1 character?

Why is an iso-8859-1 character not converted to utf-8 in the output file when setting output encoding to utf-8?
I have an xml input file in iso-8859-1 encoding, and the encoding is declared. I want to output it in utf-8. My understanding is that setting the output encoding in the xslt file should manage the character conversion.
Is my understanding wrong? If not, why does the following simple test case output an iso-8859-1 character in a utf-8 declared output file?
My input file looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>ö</data>
My transform looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output encoding="UTF-8" />
<xsl:template match="/">
<result>
<xsl:value-of select="." />
</result>
</xsl:template>
</xsl:stylesheet>
Using saxon9he from the command line my result looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<result>ö</result>
The ö in my result file is 0xF6 according to BabelPad, which is an invalid utf-8 character. The ö seems to be untouched by the transformation.
Thanks for any help!
I can see two possible explanations (thought there are probably others).
(a) the final stage of serialization, that is, converting characters to bytes, is not being done by the XSLT processor but by some other piece of software that does not have access to the stylesheet. This would happen, for example, if you run the transformation in a Java application that sends the output to a Writer rather than an OutputStream - the Writer would convert characters to bytes using the platform default encoding, which is probably iso-8859-1.
(b) the octets you are seeing in your display are not the octets stored on disk, but some transformation of them. This can happen when you load a file into an editor and then ask for a hex display; in some cases you will get a hex display of the editor's in-memory representation of the document, not of what is stored on disk.

XSLT whitespace elements getting trimmed

Quick question. I have some XML
<someXML>
<someNode> </someNode>
<someNode>asdlkjf </someNode>
</someXML>
When i apply an XSLT to this the first node is getting trimmed into nothing. The second is fine and the trailing whitespace is not trimmed because i'm preserving whitespace:
<someXML>
<someNode></someNode>
<someNode>asdlkjf </someNode>
</someXML>
My question is why is the first node getting truncated? As absurd as this sounds, the whitespace node is important and needs to be maintained. I'm using Xalan 2.7.1. Is this just the way XSL works or is there a way around this?
Thanks!
Use this XSLT directive:
<xsl:preserve-space elements="*"/>
If this doesn't help, this means that the XML parser is stripping the whitespace-only text nodes.
To prevent this, in the XML document use the xml:space="preserve" attribute:
<someXML>
<someNode xml:space="preserve"> </someNode>
<someNode>asdlkjf </someNode>
</someXML>