Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Xml Declaration, or select xml without declaration section

Tags:

c#

.net

xml

xpath

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"?>
like image 388
ganders Avatar asked Apr 24 '12 13:04

ganders


People also ask

Is XML declaration necessary?

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.

Which of the following are correct rule for XML declaration?

The correct order is: version, encoding and standalone. Either single or double quotes may be used.

Is XML optional declaration?

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.

What is the proper syntax for the declaration that specifies the XML version?

1 Answer. (a) <? xml version = “1.0”?> is a correct syntax of the declaration.


2 Answers

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.

like image 176
Irfan Avatar answered Sep 30 '22 01:09

Irfan


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" />
like image 27
Dimitre Novatchev Avatar answered Oct 02 '22 01:10

Dimitre Novatchev