Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to convert XML document to several XML documents in a new format

Status
Not open for further replies.

NotoriousBDE

Technical User
Jun 13, 2002
8
0
0
US
I'm migrating data into a content management system and in order to use the import tool provided I need to change the format of a large xml file and convert each entry to a seperate xml file.

This is what I have now:
Code:
<?xml version="1.0" encoding="UTF-8" ?> 

  <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2005-04-18T12:05:51">
  <Checklist>
  <ID>1</ID> 
  <Category>stuff</Category> 
  <Type>stuff</Type> 
  <Item>stuff</Item> 
  <Steps>stuff</Steps> 
  </Checklist>
  <Checklist>
  <ID>2</ID> 
  <Category>stuff</Category> 
  <Type>stuff</Type> 
  <Item>stuff</Item> 
  <Steps>stuff</Steps> 
  </Checklist>
  <Checklist>
  <ID>3</ID> 
  <Category>stuff</Category> 
  <Type>stuff</Type> 
  <Item>stuff</Item> 
  <Steps>stuff</Steps> 
  </Checklist>
  <Checklist>
  <ID>4</ID> 
  <Category>stuff</Category> 
  <Type>stuff</Type> 
  <Item>stuff</Item> 
  <Steps>stuff</Steps> 
  </Checklist>
  </dataroot>


This is what I need each entry to be as a single file:
Code:
<?xml version="1.0" ?> 
  <file DocType="TypeName" DocTitle="TITLE:testing 123" DocDesc="TITLE:testing 123">
  <section name="Category">
  <![CDATA[ 
stuff
  ]]> 
  </section>
  <section name="Type">
  <![CDATA[ 
stuff
  ]]> 
  </section>
  <section name="Item">
  <![CDATA[ 
stuff
  ]]> 
  </section>
  <section name="SLA">
  <![CDATA[ 
stuff
  ]]> 
  </section>
  <section name="Troubleshooting Steps">
 <![CDATA[ 
stuff
  ]]> 
  </section>
  </file>

I need some direction on how to accomplish this task...
 
You'll need to write a simple program. Which language is best for you? I could post an example.

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Hmmm, turned out to be harder than I thought. I don't like .Net:
Code:
  Sub Main()
    ' Load the stylesheet
    Dim xslt As XslTransform = New XslTransform
    xslt.Load("test.xsl")

    ' Load the XML
    Dim xml As XmlDocument = New XmlDocument
    xml.Load("test.xml")

    ' Transform each checklist into new file
    Dim i As Integer = 1
    Dim checklists As XmlNodeList = xml.SelectNodes("*/Checklist")
    For Each node As XmlNode In checklists
      xml.RemoveAll()
      xml.AppendChild(node)
      Dim strmTemp As FileStream = File.Create("test" + i.ToString + ".xml")
      xslt.Transform(xml, Nothing, strmTemp, Nothing)
      strmTemp.Close()
      i += 1
    Next
  End Sub
test.xsl:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <file DocType="TypeName" DocTitle="TITLE:testing 123" DocDesc="TITLE:testing 123">
      <section name="Category">
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:value-of select="*/Category"/>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </section>
      <section name="Type">
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:value-of select="*/Type"/>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </section>
      <section name="Item">
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:value-of select="*/Item"/>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </section>
      <section name="SLA">
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:value-of select="*/ID"/>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </section>
      <section name="Troubleshooting Steps">
        <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
        <xsl:value-of select="*/Steps"/>
        <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
      </section>
    </file>
  </xsl:template>
</xsl:stylesheet>

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Great! I will try this. I really do appreciate your assistance as I've posted this same issue in 4 other forums and 3 news groups and you're the first to respond.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top