dhruv_x0
21st November 2018, 16:02
Hello All,

Need one help in fixing the xml file which i am generating from LN program. Line 1 generated in xml file is below:

<?xml version="1.0"?>

I don't want this line in my xml file, instead of this i need below data. Any idea how to achieve this?

<docsets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:SsproNamespace"
xsi:schemaLocation="urn:SsproNamespace ssdataDXMschema_09.xsd">


Thanks In Advance

bhushanchanda
22nd November 2018, 09:10
That tag on the top is generated by default when you write an XML to a file.


To remove it you have two options -



1. Once the file is generated, read the file and remove the first line and save it again.
2. Once you create the first node, write it to a string and then user str.replace$ to remove the part you don't want and then write that string to your file.


e.g.


long parentnode
long ns
string oldstr(3000)
string newstr(3000)
string toreplace(100)

parentnode = xmlNewNode ("docsets")
ns = xmlNewNamespace( parentnode, "xsi", "http://www.w3.org/2001/XMLSchema-instance" )
ns = xmlNewNamespace( parentnode, "", "urn:SsproNamespace" )
ns = xmlNewNamespace( parentnode, "schemaLocation", "urn:SsproNamespace ssdataDXMschema_09.xsd" )

xmlWriteToString (oldstr, parentnode)
oldstr = trim$(oldstr)
toreplace = "<?xml version=" & chr$(34) & "1.0" & chr$(34) & "?>"
newstr = str.replace$(oldstr,toreplace,"")


Here I am generating the first node without the default XML tag by replacing it with a blank space.



See if this helps you.

dhruv_x0
22nd November 2018, 16:20
Thanks Bhushan for your reply. I tried 2nd option and still getting the default xml tag so now trying the 1st option. Can we achieve something like that reading the first line of a file and replace it with some other string. For doing this i believe we need to create a new file and write first file data to new file with new string as first line.

bhushanchanda
22nd November 2018, 20:18
Why didn't option 2 worked for you? Once you replace the required line, just put it in a file using seq.puts() instead of xml.write. Do that for all the other tags.

For option one, yes, you can write a small shell/batch script to remove the first line of your XML file. Google it and you will find a lot of threads.

dhruv_x0
27th November 2018, 19:58
It worked and i am able to remove this first line now. One small issue and just wanted to know if there is standard function to achieve that.

As suggested I am using below query to create the namespace

cmfID = xmlNewNode("docsets")
ns = xmlNewNamespace( cmfID, "xsi", "http://www.w3.org/2001/XMLSchema-instance" )
ns = xmlNewNamespace( cmfID, " ", "urn:SsproNamespace" )
ns = xmlNewNamespace( cmfID, "schemaLocation", "urn:SsproNamespace ssdataDXMschema_09.xsd" )


Output from this is below

<docsets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:SsproNamespace"
xmlns:schemaLocation="urn:SsproNamespace ssdataDXMschema_09.xsd">

In last line instead of "xmlns" marked in blue i need "xsi" to make file readable from another tool . Is there any standard function to achieve that? I noticed xmlns is coming default if we are creating any namespace.

Thank You in Advance.

giggty
28th November 2018, 08:09
How about

cmfID = xmlNewNode("docsets")
ns1 = xmlNewNamespace( cmfID, "xsi", "http://www.w3.org/2001/XMLSchema-instance" )
ns2 = xmlNewNamespace( cmfID, " ", "urn:SsproNamespace" )
ret = xmlSetAttributeNs( cmfID, ns1, "schemaLocation", "urn:SsproNamespace ssdataDXMschema_09.xsd" )

dhruv_x0
29th November 2018, 15:37
Thank You very much, it worked pretty well. Still got one issue in the namespace which is not letting me read this file.
Below is the code which is i am using for building xml namespace.
ns1 = xmlNewNamespace( cmfID, "xsi", "http://www.w3.org/2001/XMLSchema-instance")
ns2 = xmlNewNamespace( cmfID, " ", "urn:SsproNamespace" )
ret = xmlSetAttributeNs( cmfID, ns1, "schemaLocation", "urn:SsproNamespace ssdataDXMschema_09.xsd" )

and i am getting the below output. I am unable to read the file because of the COLON : present after xmlns in line 2 marked in blue. I need to remove the first colon: from line 2. Looked into the xml help functions but couldn't find anything to remove this. Is there any standard function to achieve this.
<docsets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:="urn:SsproNamespace"
xsi:schemaLocation="urn:SsproNamespace ssdataDXMschema_09.xsd">

Appreciate all your help on this so far.

Thank You.

giggty
29th November 2018, 17:23
Like this?


ns1 = xmlNewNamespace( cmfID, "xsi", "http://www.w3.org/2001/XMLSchema-instance")
xmlSetAttribute( cmfID, "xmlns", "urn:SsproNamespace" )
ret = xmlSetAttributeNs( cmfID, ns1, "schemaLocation", "urn:SsproNamespace ssdataDXMschema_09.xsd" )