I have the following XML string generated in code. I tried to parse this XML string using LXML:
taskMetaDataXmlDoc = etree.fromstring(ta.GetXMLString())
where ta.GetXMLString() returns the following XML string below:
<TaskMetadata xmlns:xlink="http://www.w3.org/2000/xmlns/" seqid="27152" id="27152-SSP" uniqueid="0be1d3a2-bdb9-4896-a2c8-802ed0c64def">
<Status>Published</Status>
<AssignedBy/>
<AssignedTo/>
<ContentReceived>
<DateReceived>2023-02-04T06:28:48.0000000+08:00</DateReceived>
<Remarks/>
</ContentReceived>
<Title>[TEST TITLE]</Title>
<Category xlink:href="Content.xml#r1c1" xlink:type="locator"/>
<ContentMetadata xlink:href="0be1d3a2-bdb9-4896-a2c8-802ed0c64def-c.xml" xlink:type="locator"/>
<HistoryMetadata xlink:href="0be1d3a2-bdb9-4896-a2c8-802ed0c64def-h.xml" xlink:type="locator"/>
<References xlink:href="0be1d3a2-bdb9-4896-a2c8-802ed0c64def-r.xml" xlink:type="locator"/>
<CreatedDate>2023-02-04T06:28:48.0000000+08:00</CreatedDate>
<CreatedBy>Scheduler</CreatedBy>
<LastModifiedDate>2023-02-04T06:28:48.0000000+08:00</LastModifiedDate>
<LastModifiedBy>Scheduler</LastModifiedBy>
<InitialPublishedBy>Scheduler</InitialPublishedBy>
<InitialPublishedDate>2023-02-04T06:28:48.0000000+08:00</InitialPublishedDate>
<LastPublishedDate>0001-01-01T00:00:00.0000000+08:00</LastPublishedDate>
<FuturePublishedDate>0001-01-01T00:00:00.0000000+08:00</FuturePublishedDate>
<FutureUnpublishedDate>0001-01-01T00:00:00.0000000+08:00</FutureUnpublishedDate>
</TaskMetadata>
However, the program returns error regarding reuse of XMLNS namespace:
XMLSyntaxError: reuse of the xmlns namespace name is forbidden, line 1, column 58 (, line 1)
I am not particularly good with XML, may I know which part of the XML string is causing this?
ta.GetXMLString()
(whatever that is) produces incorrect XML.
The xmlns
prefix is bound to the http://www.w3.org/2000/xmlns/
namespace name. However, that prefix must not be declared in an XML document, and other prefixes (such as xlink
) must not be bound to the namespace name.
See https://www.w3.org/TR/REC-xml-names/#ns-decl:
The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name
http://www.w3.org/2000/xmlns/
. It MUST NOT be declared. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace. Element names MUST NOT have the prefixxmlns
.
The correct namespace name for XLink is http://www.w3.org/1999/xlink
. If the namespace declaration is changed to xmlns:xlink="http://www.w3.org/1999/xlink"
, the error goes away.