Need to put XML Schema spec in XML document

Hi,

With the code below, I generate a XML file. It works well.
However, I need to put XML Schema specification in the head of XML document.
The generated XML doesn’t include that information.

Can you show me how to put the spec information in XML?

[code]
Dim xml As New XmlDocument

Dim root As XmlNode
root = xml.AppendChild(xml.CreateElement(“beans”))

Dim beanNode As XmlNode
Dim propertyNode As XmlNode

// Create 1st bean and its properties
beanNode = root.AppendChild(xml.CreateElement(“bean”))
beanNode.SetAttribute(“id”, “plan”)
beanNode.SetAttribute(“class”, “org.kt.d.conf.pn”)

propertyNode = beanNode.AppendChild(xml.CreateElement(“property”))
propertyNode.SetAttribute(“name”, “lname”)
propertyNode.SetAttribute(“value”, ST)

propertyNode = beanNode.AppendChild(xml.CreateElement(“property”))
propertyNode.SetAttribute(“name”, “rname”)
propertyNode.SetAttribute(“value”, TT)

propertyNode = beanNode.AppendChild(xml.CreateElement(“property”))
propertyNode.SetAttribute(“name”, “linfo”)
propertyNode.SetAttribute(“ref”, “linfo”)

propertyNode = beanNode.AppendChild(xml.CreateElement(“property”))
propertyNode.SetAttribute(“name”, “rinfo”)
propertyNode.SetAttribute(“ref”, “rinfo”)

propertyNode = beanNode.AppendChild(xml.CreateElement(“property”))
propertyNode.SetAttribute(“name”, “spath”)
propertyNode.SetAttribute(“value”, “./result”)

Dim prettyXML As String = xml.Transform(kPrettyPrintXSL)
'TextArea1.Text = xml.ToString
Textarea2.text = prettyXML[/code]

Generated XML

  <?xml version="1.0" encoding="UTF-8"?>

  <beans xmlns:xml="http://www.w3.org/XML/1998/namespace">

  <bean id="plan" class="org.kt.d.conf.pn">
  <property name="lname" value="TEST"/>
  <property name="rname" value="TEST"/>
  <property name="linfo" ref="linfo"/>
  <property name="rinfo" ref="rinfo"/>
  <property name="spath" value="./result"/>
  </bean>
  </beans>

Required XML

<?xml version="1.0" encoding="UTF-8"?>

 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  
  <bean id="plan" class="org.kt.d.conf.pn">
  <property name="lname" value="TEST" />
  <property name="rname" value="TEST" />
  <property name="linfo" ref="linfo" />
  <property name="rinfo" ref="rinfo" />
  <property name="spath" value="./result" />
  </bean>
  
  </beans>

root.setAttribute “xsi:http://www.w3.org/2001/XMLSchema-instance”, “xsi:schemaLocation”, “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

the first time you use a name space (xsi in your case) you have to define it using the 3 parameter setAttribute call

I put the code “root.setAttribute” after xml.CreateElement as following.

[code]Dim root As XmlNode
root = xml.AppendChild(xml.CreateElement(“beans”))

root.setAttribute “xsi:http://www.w3.org/2001/XMLSchema-instance”, “xsi:schemaLocation”, “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd” [/code]

Generated XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xml="http://www.w3.org/XML/1998/namespace" 
xmlns:xsi="xsi:http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="plan" class="org.kt.d.conf.pn">
  <property name="lname" value="TEST" />
  <property name="rname" value="TEST" />
  <property name="linfo" ref="linfo" />
  <property name="rinfo" ref="rinfo" />
  <property name="spath" value="./result" />
  </bean>
  </bean>
</beans>

However, it looks weird, and I think another “xmlns” should be added.
xmlns:xsi=“xsi:http://www.w3.org/2001/XMLSchema-instance

xmlns=“http://www.springframework.org/schema/beans

I have tried a lot of times but no success.
Can you help me more?

the first xsi: was a typo, sorry

root.setAttribute “http://www.w3.org/2001/XMLSchema-instance”, “xsi:schemaLocation”, “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Okay. Almost done.

I see xmlns:xml=“http://www.w3.org/XML/1998/namespace” is added automatically, but instead I need the below one.
xmlns=“http://www.springframework.org/schema/beans

As a novice of XML Document, not easy to make it.
Please kindly help me out.

Generated Namespace.

<beans xmlns:xml="http://www.w3.org/XML/1998/namespace" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

Required Namespace

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

the xmlns:xml is written by the xslt trasformation
you can try to use:
<xsl:output method=“xml” encoding=“utf-8” omit-xml-declaration=“yes” indent=“yes”/>

you can add
xmlns=“http://www.springframework.org/schema/beans

as a simple attribure (it’s not a named name space)

It works great now.

Thank you so much.

[quote=229725:@Antonio Rinaldi]root.setAttribute “xsi:http://www.w3.org/2001/XMLSchema-instance”, “xsi:schemaLocation”, “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

the first time you use a name space (xsi in your case) you have to define it using the 3 parameter setAttribute call[/quote]
Thanks, It Works!