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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to Modify of a existing XML file... how to?

Status
Not open for further replies.

sugarferret

IS-IT--Management
Jul 11, 2005
33
US
H i , 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

Live as a tortoise.
and rate my mullet:
 
That might not be a very good idea to have a data xml with element referencing specifically with the number of entries of itself. That would impose a rigid constraint on the xml when later the file be updated or changed.

One way to do it is this.
[tt]
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="@*|node()">
<xsl:if test="local-name(.) = 'Quantity'">
<xsl:element name="ItemNumber">
<xsl:value-of select="count(parent::*/preceding-sibling::*)" />
</xsl:element>
</xsl:if>
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Further note:
If you make numbering 1-based, then modify the corresponding line.
[tt] <xsl:value-of select="count(parent::*/preceding-sibling::*)[blue]+1[/blue]" />
[/tt]
 
I agree with tsuji(!). What is the point of numbering the Items? They inherently have numbering already - the position in the nodeset.

Jon

"I don't regret this, but I both rue and lament it.
 
I think any of the solutiuons can help me, the tsuji's one and the inherits tip of JontyMC, in any case how can I run the xsl code in order to modify the xml file?

or how can I call the inherited node numbers?



Live as a tortoise.
and rate my mullet:
 
>I think any of the solutiuons can help me, the tsuji's one and ...
That only means you are not prepared yourself to get help.
 
I depends. How do you want to modify the xml file?

Say you want to select the 3rd item, simply use: item[3]

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top