How to modify xml declaration in XSLT - xslt

I want to modify processing instructions in a source xml with XSLT, for example:
XML INPUT
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
XML OUTPUT
<?xml version="1.0" encoding="WINDOWS-1252"?>
<root>
</root>
Can I do this with XSLT?
thanks in advance.

That is the XML declaration, it is not a processing instruction. If you want a particular output encoding then use e.g. <xsl:output encoding="Windows-1252"/>. But any XML parser is required to support UTF-8 so using an 8-bit code page in the age of Unicode and XML does not improve interoperability.

Related

XML value contains xml characters

Actually an XML is passed as a value of Input XML attribute. Please help to fetch the XML value using XSLT 1.0
<Root>
<Element1 ProductDetails="<Input xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Amount="15632" Product="Pencil"></Input>"/>
</Root>
I need to just fetch the value of ProductDetails attribute.
i.e. only the below part using XSLT 1.0
<Input xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Amount="15632" Product="Pencil"></Input>
check it:-
<xsl:value-of select="#ProductDetails"/>

How to use XSLT when my XML contains a Prefix?

I have this XML below and I'm trying to print the content of the MyField element using XSLT. Unfortunately I can't make it work. Can you help me fix my XSLT?
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="my_xsl.xsl"?>
<ns:Collection>
<Load>
<Item>
<MyField>Please Print Me</MyField>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="ns:Collection/Load/Item/MyField" />
Your XML is not namespace well formed because the namespace prefix "ns" has not been bound to any namespace URI.
XSLT cannot process XML input unless it is namespace well formed.
Use namespace for ns: in the root element

Check for XML header

How can i check in XSLT that an XML contains XML headers?
Example XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Data>
<test>1</test>
</Data>
I want to check if the <?xml/> header exists.
<xsl:if test="xml header exists">
do something
</xsl:if>
// otherwise?
Thanks
The <?xml version="1.0" encoding="UTF-8" standalone="yes"?> is called the XML declaration and it is not part of the XSLT/XPath/XQuery data model so you can't access it or check for it using XSLT/XPath/XQuery.

How to encode(escaping special characters) in XSLT1.0 using Apache Software Foundation (Xalan XSLTC) Processor

I am using XSLT 1.0, Apache Software Foundation (Xalan XSLTC) processor. I am not supposed to use XSLT 2.0. I have the following xml in which the value of <prvNum> has some special characters.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<prvNum>SPECIAL#1&</prvNum>
</root>
Now I want to perform percent-encoding for the value of <prvNum>. For example the value should be changed as below after percent encoding:
SPECIAL%231%26
My output xml should looklike below:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<prvNum>SPECIAL%231%26</prvNum>
</root>
Can anybody tell me how do that?

How to add cxml doctype using XSLT

I am generating correct cxml but doctype is missing in the beginning.
How can I add the below doc type after <?xml version="1.0" encoding="utf-8"?> and copy the remaining xml using XSLT mapping.
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.032/cXML.dtd">
Desired output should be like
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.032/cXML.dtd">
<xml>
<field>.... (followed by remaining xml structure)
Thanks,
Varun
Try
<xsl:output method="xml"
doctype-system="http://xml.cxml.org/schemas/cXML/1.2.032/cXML.dtd"
/>