Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: New to Schemas

From: "tshad" <tfs@----------.--->
To: NULL
Date: 1/2/2008 8:11:00 AM

"Martin Honnen" <mahotrash@y...> wrote in message 
news:umrYot7SIHA.4752@T......
> tshad wrote:
>> I am trying to read an xml document into a dataSet and am unsure how to 
>> set it up.
>>
>> If I have a document such as:
>>
>> <?xml version="1.0"?>
>> <address country="Italy">
>>     <tag name="street" flag="5">One Microsoft Way</tag>
>>     <tag name="number">1</tag>
>>     <tag name="city">Redmond</tag>
>>     <tag name="state" unit="25">WA</tag>
>>     <tag name="zip">98052</tag>
>> </address>
>>
>> I want there to be one table: Address and each record would be the "tag" 
>> lines.  The line would have a name (such as "street" or "city") a value 
>> (such as ""One Microsoft Way") and a possible attribute or 2 (such as 
>> flag).
>>
>> I would want the table to be something like:
>>
>> street       5        One Microsoft Way
>> number              1
>> city                   Redmond
>> state            25 WA
>> zip                    98052
>
> One way to solve that is to use an XSLT stylesheet to transform the XML to 
> the format ReadXml expects. Here is an XSLT stylesheet that does that:
>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>   <xsl:output method="xml" indent="yes"/>
>   <xsl:template match="address">
>     <dataset>
>       <xsl:apply-templates/>
>     </dataset>
>   </xsl:template>
>   <xsl:template match="tag">
>     <address>
>       <name>
>         <xsl:value-of select="@name"/>
>       </name>
>       <flag>
>         <xsl:if test="not(@flag)">
>           <xsl:attribute name="xsi:nil">true</xsl:attribute>
>         </xsl:if>
>         <xsl:value-of select="@flag"/>
>       </flag>
>       <unit>
>         <xsl:if test="not(@unit)">
>           <xsl:attribute name="xsi:nil">true</xsl:attribute>
>         </xsl:if>
>         <xsl:value-of select="@unit"/>
>       </unit>
>       <value>
>         <xsl:value-of select="."/>
>       </value>
>     </address>
>   </xsl:template>
> </xsl:stylesheet>
>
> You can then apply the transformation with XslCompiledTransform as 
> follows:
>
>             XslCompiledTransform xsltProcessor = new 
> XslCompiledTransform();
>             xsltProcessor.Load(@"..\..\XSLTFile1.xslt");
>             DataSet ds = new DataSet();
>             using (MemoryStream memStream = new MemoryStream())
>             {
>                 xsltProcessor.Transform(@"..\..\XMLFile1.xml", null, 
> memStream);
>                 memStream.Position = 0;
>                 ds.ReadXml(memStream);
>             }
>
> That way you get one table with five rows:
>
> Table address:
> name    flag    unit    value
> street  5               One Microsoft Way
> number                  1
> city                    Redmond
> state           25      WA
> zip                     98052
>
>

That worked really well.

How would you change this if you have a set of tags inside of the Address 
tags (Section), such that if there is a section tag there would be a section 
field using the "Comp" number (from my example) in each of the row for that 
section.  And the type - but that wouldn't need to be in each row - just at 
the beginning row.

I need to differentiate the addresses as well as have different addresses 
(perhaps add a counter to the row that tells me I have a new address such 
that all the rows in the first address have a number one and the next has a 
number).

So that if I have the following:

<?xml version="1.0"?>
<address country="Italy">
    <tag name="street" flag="5">One Microsoft Way</tag>
    <tag name="number">1</tag>
    <tag name="city">Redmond</tag>
    <tag name="state" unit="25">WA</tag>
    <tag name="zip">98052</tag>
</address>
<address country="France">
  <section type="Condo" Comp="1">
    <tag name="street">15 Mako Place</tag>
    <tag name="number">1545</tag>
    <tag name="city">Fargo</tag>
    <tag name="state" unit="10">Paris</tag>
    <tag name="zip">10110</tag>
  </section>
  <section type="Condo" Comp="2">
    <tag name="street" flag="123">25 Gregson </tag>
    <tag name="number">2341</tag>
    <tag name="city">Paris</tag>
    <tag name="state" unit="150">Paris</tag>
    <tag name="zip">10111</tag>
  </section
  <section type="Townhouse" Comp="3">
    <tag name="street">152 Madison </tag>
    <tag name="number">345</tag>
    <tag name="city">Paris</tag>
    <tag name="state">Paris</tag>
    <tag name="zip">10222</tag>
  </section
</address>

I would have something like:

name    Comp  flag    unit    value                          Country 
Type
1 street       5        One Microsoft Way     Italy
1 number              1
1 city                    Redmond
1 state            25  WA
1 zip                    98052
2  1   street                15 Mako Place            France   Condo
2  1   number              1545
2  1   city                   Redmond
2  1   state            10 Paris
2  1   zip                    10110
3  2   street     123    25 Gregson                  France   Condo
3  2   number            2341
3  2   city                   Redmond
3  2   state            150 Paris
3  2   zip                    10111
4  3   street                152 Madison               France   Townhouse
4  3   number              345
4  3   city                   Redmond
4  3   state                Paris
4  3   zip                    10222

Also, If I had a different set of tags following the address, such as 
Images, can that be put in a different table in the Dataset or would I need 
to run a different transformation on it with a different style sheet?

Thanks,

Tom
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/ 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent