Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-09-2006, 12:00 AM   PM User | #1
sugar2
New Coder

 
Join Date: May 2005
Location: California
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
sugar2 is an unknown quantity at this point
Automated Modify of a existing XML file... how to?

Hi, I have few xml files with this format: it contains supplier info and a list of products.... but the products are not numbered (such as #1, #2, #3, etc)
Code:
  <Item>
     <Quantity>12</Quantity>
     <Code>20001318</Code>
     <TotalPrice>27.12</TotalPrice>
  </Item>
How can (or if is there a good tool to do that) batch number each item of the xml file? the result xml i wish to have is some like:
Code:
<Items>
   <Item>
      <ItemNumber>1</ItemNumber>
      <Quantity>12</Quantity>
      <Code>20001318</Code>
      <TotalPrice>27.12</TotalPrice>
   </Item>
   <Item>
      <ItemNumber>2</ItemNumber>
      <Quantity>2</Quantity>
      <Code>77701318</Code>
      <TotalPrice>27.12</TotalPrice>
   </Item>
   <Item>
      <ItemNumber>3</ItemNumber>
      <Quantity>1</Quantity>
      <Code>11111318</Code>
      <TotalPrice>27.12</TotalPrice>
   </Item>
</Items>
Thanks in advance
sugar2 is offline   Reply With Quote
Old 03-09-2006, 12:47 AM   PM User | #2
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,428
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
Hmmm. How are these individual XML files stored?

My instinctive reaction is to write something like this:

Code:
<Items>
<?php
include("item1.xml");
include("item2.xml");
/* etc. etc. */
?>
</Items>
You'll probably have to do some tinkering to get it right, though.
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
June 30, 2001
author, Verbosio prototype XML Editor
author, JavaScript Developer's Dictionary
https://alexvincent.us/blog
Alex Vincent is offline   Reply With Quote
Old 03-09-2006, 12:53 AM   PM User | #3
sugar2
New Coder

 
Join Date: May 2005
Location: California
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
sugar2 is an unknown quantity at this point
probably it doesnt made sense... its a single large xml file with dozens of items, such as:
Code:
<items>
  <Item>
  < # I need to insert a number tag here, such as ItemNumber=1>
bla bla bla
  </Item>

  <Item>
  < # I need to insert a number tag here, such as ItemNumber=2>
bla bla bla
  </Item>

  <Item>
  < # I need to insert a number tag here, such as ItemNumber=3>
bla bla bla
  </Item>

  <Item>
  < # I need to insert a number tag here, such as ItemNumber=4>
bla bla bla
  </Item>

  <Item>
  < # I need to insert a number tag here, such as ItemNumber=5>
bla bla bla
  </Item>

</items>
Is that possible in any way?
Thanks again
sugar2 is offline   Reply With Quote
Old 03-09-2006, 01:39 AM   PM User | #4
KC-Luck
Regular Coder

 
Join Date: Aug 2005
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KC-Luck is an unknown quantity at this point
if you add a xml-stylesheet processing-instruction,
Code:
<?xml-stylesheet type="text/xsl" href="numberedItems.xsl"?>
or perform a transform with this stylesheet.

numberedItems.xsl
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  
  <xsl:template match="/Items">
    <Items>
      <xsl:apply-templates/>
    </Items>
  </xsl:template>

  <xsl:template match="Item">
    <Item>
      <ItemNumber><xsl:value-of select="position()"/></ItemNumber>
      <xsl:copy-of select="*"/>
    </Item>
  </xsl:template>

</xsl:stylesheet>
KC-Luck is offline   Reply With Quote
Old 03-09-2006, 01:53 AM   PM User | #5
sugar2
New Coder

 
Join Date: May 2005
Location: California
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
sugar2 is an unknown quantity at this point
KC-luck, Thanks for the replay.... as you can see I am prety new with XML processing, and is the first time i ear about xml - stylesheet processing instructions...
I am proccessing the xml file with a useful command called xml2odbc, could you please guideme of how to implement the solution you provided me?
I think the fist thing i should do is modify the first row of my xml file:

from <?xml version="1.0" encoding="UTF-16"?>
to <?xml-stylesheet type="text/xsl" href="numberedItems.xsl"?>

is that rigth?

now, in the seccond step where should i place the file called "numberedItems.xsl" ?
That solution can be implemented even if I deal with that files offline? or its mandatory to be on a web server or some like that?

Last edited by sugar2; 03-09-2006 at 02:40 AM..
sugar2 is offline   Reply With Quote
Old 03-09-2006, 02:39 PM   PM User | #6
KC-Luck
Regular Coder

 
Join Date: Aug 2005
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KC-Luck is an unknown quantity at this point
Quote:
I am proccessing the xml file with a useful command called xml2odbc
I do not know that the xml2odbc command is, or where you would be using that from?

But the stylesheet I supplied would be used first to simply reformat the existing xml document, then you'd use the results to send to your xml2odbc command.

Quote:
from <?xml version="1.0" encoding="UTF-16"?>
to <?xml-stylesheet type="text/xsl" href="numberedItems.xsl"?>
you do not need to replace the first declaration, but simply add the "processing-instruction" below the xml declaration instruction.

Code:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="numberedItems.xsl"?>
or, if you have any other mechanism for applying the XSLTransform ("numberedItems.xsl" stylesheet), and then saving the results directly out.

This can be easily done via an IDE such as XMLSpy, or through some simply coding in a MS HTA Application/Windows Script Host, etc.
KC-Luck is offline   Reply With Quote
Old 03-09-2006, 09:24 PM   PM User | #7
sugar2
New Coder

 
Join Date: May 2005
Location: California
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
sugar2 is an unknown quantity at this point
Im not sure if i have installed the XSLTransform command in my machine... im prety interested in how to accomplish the XSLTransform ("numberedItems.xsl" stylesheet) directly with the DOS shell command, or VB, or WSH... could you please tellme where can I get the XSLTransform tool?

I found this :
Code:
[Visual Basic]
NotInheritable Public Class XslTransform
and the sample:
Code:
[Visual Basic] 
'Create a new XslTransform object.
Dim xslt As New XslTransform()

'Load the stylesheet.
xslt.Load(CType("http://server/favorite.xsl", String))

'Create a new XPathDocument and load the XML data to be transformed.
Dim mydata As New XPathDocument("inputdata.xml")

'Create an XmlTextWriter which outputs to the console.
Dim writer As New XmlTextWriter(Console.Out)

'Transform the data and send the output to the console.
xslt.Transform(mydata, Nothing, writer, Nothing)
thanks

Last edited by sugar2; 03-09-2006 at 09:27 PM..
sugar2 is offline   Reply With Quote
Old 03-09-2006, 10:37 PM   PM User | #8
KC-Luck
Regular Coder

 
Join Date: Aug 2005
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KC-Luck is an unknown quantity at this point
with WSH it is very simple.
esp when doing a direct xml -> xml transform.

transform.js
Code:
function XML(src) {
  var obj = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
  obj.async = false;
  obj.resolveExternals = false;
  obj.validateOnParse = false;
  obj.load(src);
  return obj;
}

var xml = XML("your-items.xml");
var xsl = XML("numberedItems.xsl");
var out = XML(); // just prep a Document for the results

xml.transformNodeToObject(xsl, out);

out.save("your-items-numbered.xml");
double click in same folder as your .xml and .xsl

g'luck
KC-Luck is offline   Reply With Quote
Old 03-09-2006, 11:29 PM   PM User | #9
sugar2
New Coder

 
Join Date: May 2005
Location: California
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
sugar2 is an unknown quantity at this point
It is returning me an error: There is no script file specified.
This is the WSH.WSH FILE:
Code:
function XML(src) {
  var obj = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
  obj.async = false;
  obj.resolveExternals = false;
  obj.validateOnParse = false;
  obj.load(src);
  return obj;
}

var xml = XML("xml.xml");
var xsl = XML("xsl.xsl");
var out = XML(); // just prep a Document for the results

xml.transformNodeToObject(xsl, out);

out.save("result.xml");
THIS IS THE XSL.XSL file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  
  <xsl:template match="/Items">
    <Items>
      <xsl:apply-templates/>
    </Items>
  </xsl:template>

  <xsl:template match="Item">
    <Item>
      <ItemNumber><xsl:value-of select="position()"/></ItemNumber>
      <xsl:copy-of select="*"/>
    </Item>
  </xsl:template>

</xsl:stylesheet>

And this is the XML.XML file:
Code:
<?xml version="1.0" encoding="UTF-16"?>
<PurchaseOrder number="0000168">
<Title>0000168</Title>
<To>Craft Supply</To>

<ShipTo>ME</ShipTo>
<Date>11/3/2005</Date>
<RequiredDate>11/3/2005</RequiredDate>
<Requisitioner></Requisitioner>
<Remarks></Remarks>
<ShipVia></ShipVia>
<FOBPoint></FOBPoint>
<Terms>Net 15</Terms>
<Freight></Freight>
<ConfirmingTo></ConfirmingTo>
<SubTotal>542.37</SubTotal>
<Shipping>0</Shipping>
<Tax>0</Tax>
<Total>542.37</Total>
<Items>
<Item><Quantity>12</Quantity><Code>20001318</Code><OrderNumber>705001-CL</OrderNumber><Description>Bead Garland 48 in. L - Clear</Description><UnitPrice>2.26</UnitPrice><TotalPrice>27.12</TotalPrice><TaxRate>0</TaxRate></Item>
<Item><Quantity>30</Quantity><Code>20008574</Code><OrderNumber>4807-LV</OrderNumber><Description>Glitter Wheat Bunch Lavender</Description><UnitPrice>1.61</UnitPrice><TotalPrice>48.3</TotalPrice><TaxRate>0</TaxRate></Item>
<Item><Quantity>20</Quantity><Code>20008578</Code><OrderNumber>4975-CR</OrderNumber><Description>10 in. Latex Poinsetta Cream</Description><UnitPrice>1.19</UnitPrice><TotalPrice>23.8</TotalPrice><TaxRate>0</TaxRate></Item>
<Item><Quantity>20</Quantity><Code>20008580</Code><OrderNumber>4975-GR</OrderNumber><Description>10 in. Latex Poinsetta Green</Description><UnitPrice>1.19</UnitPrice><TotalPrice>23.8</TotalPrice><TaxRate>0</TaxRate></Item>
<Item><Quantity>20</Quantity><Code>20008581</Code><OrderNumber>4975-PK</OrderNumber><Description>10 in. Latex Poinsetta Pink</Description><UnitPrice>1.19</UnitPrice><TotalPrice>23.8</TotalPrice><TaxRate>0</TaxRate></Item>
<Item><Quantity>20</Quantity><Code>20008582</Code><OrderNumber>4975-LV</OrderNumber><Description>10 in. Latex Poinsetta Lavende</Description><UnitPrice>1.19</UnitPrice><TotalPrice>23.8</TotalPrice><TaxRate>0</TaxRate></Item>
<Item><Quantity>23</Quantity><Code>20008583</Code><OrderNumber>4803-GR</OrderNumber><Description>Glittered Leaf Spray Green</Description><UnitPrice>1.19</UnitPrice><TotalPrice>27.37</TotalPrice><TaxRate>0</TaxRate></Item>
</Items>
</PurchaseOrder>
Is something wrong?
Thanks in advance.
sugar2 is offline   Reply With Quote
Old 03-10-2006, 12:09 AM   PM User | #10
KC-Luck
Regular Coder

 
Join Date: Aug 2005
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KC-Luck is an unknown quantity at this point
yes, your xml is way different then what you posted earlier.
i'll revise the xsl in a moment to show how to fix this.

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  
  <xsl:template match="Item">
    <Item>
      <ItemNumber><xsl:value-of select="position()"/></ItemNumber>
      <xsl:copy-of select="*"/>
    </Item>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:element name="{local-name()}">
      <xsl:if test="node()">
        <xsl:apply-templates/>
      </xsl:if>
    </xsl:element>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="comment()|processing-instruction()"/>

</xsl:stylesheet>

Last edited by KC-Luck; 03-10-2006 at 12:27 AM..
KC-Luck is offline   Reply With Quote
Old 03-10-2006, 06:35 PM   PM User | #11
sugar2
New Coder

 
Join Date: May 2005
Location: California
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
sugar2 is an unknown quantity at this point
thanks!!!!!
It is really modifying the xml file!
wow

Thanks a lot!

Aldo

Last edited by sugar2; 03-11-2006 at 07:10 PM..
sugar2 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:05 PM.


Advertisement
Log in to turn off these ads.