Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Converting hierarchical xml to vfp
Message
De
18/08/2002 09:39:22
 
 
À
16/08/2002 12:03:57
Information générale
Forum:
Visual FoxPro
Catégorie:
XML, XSD
Divers
Thread ID:
00690345
Message ID:
00690853
Vues:
12
>>I have invoices in a hierarchical xml format:
>>
>>
>><?xml version="1.0" encoding="ISO-8859-1"?>
>><invoice>
>>    <number>11009</number>
>>    <date>20020801</date>
>>    <rows>
>>        <row>
>>            <item>80686001508</item>
>>            <quantity>2</quantity>
>>        </row>
>>        <row>
>>            <item>2136861443</item>
>>            <quantity>4.5</quantity>
>>        </row>
>>    </rows>
>></invoice>
>>
>>
>>I need to create a vfp dbf file from this file.
>>
>>I tried
>>XMLTOCURSOR('invoice.xml','invoice',512)
>>but almost all data is missing from the result cursor.
>>
>>How to to convert this xml file to a vfp ?
>>I need a singe dbf file or separate dbf files.
>
>To use XmlToCursor() you will need some help of XSL Transformation and XML DOM. Check this:
>
>
>source = CreateObject('MSXML.Domdocument')
>source.loadXml(FileToStr('source.xml'))
>stylesheet = CreateObject('MSXML.Domdocument')
>stylesheet.loadXML(FileToStr('invoice.xsl'))
>result = CreateObject('MSXML.Domdocument')
>result.validateOnParse = .t.
>source.transformNodeToObject(stylesheet, @result)
>XMLToCursor(result.xml, 'Invoice')
>Browse
>
>
>This assumes that source.xml is file with the above xml structure and with the invoice data. Invoice.xsl is gile with the following content:
>
><?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
><xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes"/>
>
><!-- this replaces root tag with VFPData which is required from CursorToXML to work properly -->
><xsl:template match="/">
><xsl:element name="VFPData">
><xsl:apply-templates/>
></xsl:element>
></xsl:template>
>
><!-- this will denormalize XML data -->
><xsl:template match="/invoice/rows/*">
><xsl:element name="invoice">
><xsl:element name="number"><xsl:value-of select="../../number"/></xsl:element>
><xsl:element name="date"><xsl:value-of select="../../date"/></xsl:element>
><xsl:element name="item"><xsl:value-of select="item"/></xsl:element>
><xsl:element name="quantity"><xsl:value-of select="quantity"/></xsl:element>
></xsl:element>
></xsl:template>
>
><!-- to ommit nodes data -->
><xsl:template match="text()">
></xsl:template>
>
><!-- to work over every node -->
><xsl:template match="*">
><xsl:apply-templates/>
></xsl:template>
>
></xsl:stylesheet>
>
>
>According to me it is the fastest and simplest approach.

Zlatin, thank you. Excellent.

I noticed that numeric fields are create for item code and invoice
numbers. However, I need character type fields for those.

How to force the creation of numberic fields ?
I tried give a schema to a xmltocursor() function as argument.
However, sometimes some fields are missing from xml source file
and this causes error.
Is it possible to force a character data type even if some field is missing
in xml file sometimes ?
Andrus
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform