I need to change the xml declaration section of by document, or just select the data minus the declaration. Which is easier?
This is an example of what my xml looks like:
<?xml version="1.0" encoding="utf-16"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master page-height="11in" page-width="8.5in" margin-top="0.50in" margin-left="0.8in" margin-right="0.8in" margin-bottom="0.25in" master-name="PageMaster">
<fo:region-body border-style="none" border-width="thin" margin-top="0in" margin-left="0in" margin-right="0in" margin-bottom="0.25in"/>
<fo:region-after border-style="none" border-width="thin" extent="0.25in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="PageMaster"/>
</fo:root>
I'm trying to change the xml declaration to be:
<?xml version="1.0" encoding="iso-8859-1"?>
The XML declaration is mandatory if the encoding of the document is anything other than UTF-8 or UTF-16. In practice, this means that documents encoded using US-ASCII can also omit the XML declaration because US-ASCII overlaps entirely with UTF-8. Only one encoding can be used for an entire XML document.
The correct order is: version, encoding and standalone. Either single or double quotes may be used.
Xml declaration is optional so your xml is well-formed without it. But it is recommended to use it so that wrong assumptions are not made by the parsers, specifically about the encoding used.
1 Answer. (a) <? xml version = “1.0”?> is a correct syntax of the declaration.
Are you trying to change the XML programmatically? If so you can do so by creating new XmlDeclaration
and replacing it with the previous one as shown below:
XmlDeclaration xmlDeclaration;
xmlDeclaration = doc.CreateXmlDeclaration("1.0", "iso-8859-1", null);
doc.ReplaceChild(xmlDeclaration, doc.FirstChild);
You just have to make sure that the first child of the document is Xml Declaration.
The wanted "change of XML declaration" can be done very easily (not commenting whether or not this change is the correct solution to your problem) using XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" encoding="ISO-8859-1"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Just apply this transformation to the provided XML document:
<?xml version="1.0" encoding="utf-16"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master page-height="11in" page-width="8.5in" margin-top="0.50in" margin-left="0.8in" margin-right="0.8in" margin-bottom="0.25in" master-name="PageMaster">
<fo:region-body border-style="none" border-width="thin" margin-top="0in" margin-left="0in" margin-right="0in" margin-bottom="0.25in"/>
<fo:region-after border-style="none" border-width="thin" extent="0.25in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="PageMaster"/>
</fo:root>
and the wanted result is produced:
<?xml version="1.0" encoding="iso-8859-1"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master page-height="11in" page-width="8.5in" margin-top="0.50in" margin-left="0.8in" margin-right="0.8in" margin-bottom="0.25in" master-name="PageMaster">
<fo:region-body border-style="none" border-width="thin" margin-top="0in" margin-left="0in" margin-right="0in" margin-bottom="0.25in" />
<fo:region-after border-style="none" border-width="thin" extent="0.25in" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="PageMaster" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With