lgbjr,
The error itself is because I used "Name" for the "name" attribute of
Attributes.
However once that is fixed you will have another problem, your original XML
did not include the "type" nodes for Attributes.
Your original XML had:
| > | <attribute Name="AttB1-1">
| > | <value>
| > | X
| > | </value>
| > | </attribute>
While your "actual" XML has:
| <attribute name="BSSId">
| <datatype>
| <string>
| <value>
| BSS_TaiZhouBSS01
| </value>
| </string>
| </datatype>
| </attribute>
Notice the <datatype> & <string> nodes in the above. Also it appears your
original includes both a document node of <top> and another node of <model>.
Here is an updated XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
<!-- handle the document/root node of the input -->
<xsl:template match="/">
<xsl:apply-templates select="*" />
</xsl:template>
<!-- copy the top node "as is" -->
<xsl:template match="top">
<xsl:element name="top">
<xsl:copy-of select="@*" />
<xsl:apply-templates select="*" />
</xsl:element>
</xsl:template>
<!-- copy the model node "as is" -->
<xsl:template match="model">
<xsl:element name="model">
<xsl:copy-of select="@*" />
<xsl:apply-templates select="*" />
</xsl:element>
</xsl:template>
<!-- convert each instance node into a node that is the same as the Type
attribute -->
<xsl:template match="instance">
<xsl:element name="{@type}">
<xsl:copy-of select="@*" />
<xsl:apply-templates select="*" />
</xsl:element>
</xsl:template>
<!-- convert each Attribute node into a node that has the same name as the
attribute -->
<xsl:template match="attribute">
<xsl:element name="{@name}">
<xsl:attribute name="xsi:type"
namespace="
http://www.w3.org/2001/XMLSchema-instance">
<xsl:choose>
<xsl:when test="local-name(datatype/*)='enumRef'">
<xsl:value-of select="datatype/*/
@name" />
</xsl:when>
<xsl
therwise>
<xsl:value-of select="local-name(datatype/*)" />
</xsl
therwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="datatype/*/value" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
NOTE: I simply copy the <top> & <model> nodes as is. I "preserve" the type
of the individual Attributes, as an attribute of that Attribute.
This:
| <attribute name="BSSId">
| <datatype>
| <string>
| <value>
| BSS_TaiZhouBSS01
| </value>
| </string>
| </datatype>
| </attribute>
Becomes this:
<BSSId type="string">BSS_TaiZhouBSS01</BSSId>
Note: The type attribute in the above, should probably be xsi:type!
Instead of:
<xsl:element name="{@name}">
<xsl:attribute name="xsi:type"
namespace="
http://www.w3.org/2001/XMLSchema-instance">
You can use:
<xsl:element name="{@name}">
<xsl:attribute name="type">
To remove all of the xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
in the output, ideally xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
should be included once on <top>.
Hope this helps
Jay
| Hi All,
|
| OK, I'm starting to understand a bit of what I need to do (and why). When
I
| add the XML file to the project, open it, then right click in the view and
| select View Data Grid, I get an error that says "The Table (instance)
cannot
| be the child table to itself in nested relations. this makes sense based
on
| the way the XML file is setup.
|
| So, I need to do as Jay recommended, which is to transform each instance
to
| its type and each attribute to its name. Then, each instance will be a
table
| (multiples of the same instance will be rows in the table) and each
| attribute in an instance will be a column in the table.
|
| So, I'm working with Jay's XSLT file as a start and am using the bit of
code
| that he supplied to load the XSLT file, then do the transform.
|
| Here's the first problem. when I run the transform, I get an exception:
|
| The empty string '' is not a valid name.
|
| And the output XML file is obviously incomplete.
|
| This is the top of the XML file that I want to transform:
|
| <?xml version="1.0" ?>
| <top>
| <model mim="CO_BSSMIM" name="MIB_BSS_BSS_TaiZhouBSS01_133146459903480000"
| prefixDN="RMA=Radio_Access_Network" release="UNDEFINED" rootLDN=""
| version="2.2">
| <instance identity="instance_1151" name="BSS_TaiZhouBSS01" type="BSS">
| <attribute name="BSSId">
| <datatype>
| <string>
| <value>
| BSS_TaiZhouBSS01
| </value>
| </string>
| </datatype>
| </attribute> <instance identity="instance_0" name="RBS_H101"
| type="RBS">
| <attribute name="RBSId">
| <datatype>
| <string>
| <value>
| RBS_H101
| </value>
| </string>
| </datatype>
| </attribute> <attribute name="RBSModel">
| <datatype>
| <enumRef name="RBSModelType">
| <value>
| 1
| </value>
| </enumRef>
| </datatype>
| </attribute> <attribute name="SectorConfiguration">
| <datatype>
| <enumRef name="SectorConfigurationType">
| <value>
| 0
| </value>
| </enumRef>
| </datatype>
| </attribute>
|
| this is the output XML file:
|
| <?xml version="1.0" encoding="utf-8"?><lgbjr>
|
| <BSS
|
| There are no trailing spaces in the input XML file. The transform is
failing
| at the first transformation. Can someone tell me why?
|
| thanks!!
|
| Lee
|
| | > lgbjr,
| > I would consider using an XSLT transform to transform your input data
into
| > something a little more DataSet friendly. Here is the start of such a
| > transform:
| >
| > <?xml version="1.0" encoding="UTF-8" ?>
| > <xsl:stylesheet version="1.0"
| > xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
| > <!-- handle the document/root node of the input -->
| > <xsl:template match="/">
| > <xsl:element name="lbgjr">
| > <xsl:apply-templates select="*" />
| > </xsl:element>
| > </xsl:template>
| > <!-- convert each instance node into a node that is the same as the Type
| > attribute -->
| > <!-- from: -->
| > <!-- <instance identity="A1" name="A1" type="A_Type"> -->
| > <!-- <instance identity="B1" name="B1" type="B_Type"> -->
| > <!-- <instance identity="C1" name="C1" type="C_Type"> -->
| > <!-- to: -->
| > <!-- <A_Type identity="A1" name="A1" type="A_Type"> -->
| > <!-- <B_Type identity="B1" name="B1" type="B_Type"> -->
| > <!-- <C_Type identity="C1" name="C1" type="C_Type"> -->
| > <xsl:template match="instance">
| > <xsl:element name="{@type}">
| > <xsl:for-each select="@*">
| > <xsl:attribute name="{local-name(.)}">
| > <xsl:value-of select="." />
| > </xsl:attribute>
| > </xsl:for-each>
| > <xsl:apply-templates select="*" />
| > </xsl:element>
| > </xsl:template>
| > <!-- convert each Attribute node into a node that has the same name as
the
| > attribute -->
| > <!-- from: -->
| > <!-- <attribute Name="AttA1-1"> -->
| > <!-- <value>X</value> -->
| > <!-- </attribute> -->
| > <!-- to: -->
| > <!-- <AttA1-1>X</AttA1-1> -->
| > <xsl:template match="attribute">
| > <xsl:element name="{@Name}">
| > <xsl:value-of select="value" />
| > </xsl:element>
| > </xsl:template>
| > </xsl:stylesheet>
| >
| > You can call it with code similar to:
| >
| > Dim xslt As New XslTransform
| > xslt.Load("lgbjr.xslt")
| > xslt.Transform("lgbjr.xml", "lgbjr.output.xml", Nothing)
| > Dim ds As New DataSet
| > ds.ReadXml("lgbjr.output.xml")
| >
| > I've only minimally tested the above template, as your sample data is
not
| > as
| > well formed as the description you gave...
| >
| > Hope this helps
| > Jay
| >
| > | > | Hi All,
| > |
| > | I've been given an XML file (12 MB) that I need to extract data from.
| > The
| > | XML file makes the data easy to view, but the data is impossible to
| > analyze
| > | in its current format. I've looked at lots of XMLTextReader stuff and
I
| > | can't seem to figure out how to extract the info that I want. Here's a
| > quick
| > | example of the XML format:
| > |
| > | <instance identity="A1" name="A1" type="A_Type">
| > | <attribute Name="AttA1-1">
| > | <value>
| > | X
| > | </value>
| > | </attribute>
| > | <instance identity="B1" name="B1" type="B_Type">
| > | <attribute Name="AttB1-1">
| > | <value>
| > | X
| > | </value>
| > | </attribute>
| > | <instance identity="B2" name="B2" type="B_Type">
| > | <attribute Name="AttB2-1">
| > | <value>
| > | X
| > | </value>
| > | </attribute>
| > | <instance identity="C1" name="C1" type="C_Type">
| > | <attribute Name="AttC1-1">
| > | <value>
| > | X
| > | </value>
| > | </attribute>
| > | </instance>
| > | </instance>
| > | </instance>
| > | </instance>
| > |
| > | Each A_Type contains several B_Types, and each B_Type contains several
| > | C_Types and the file contains lots of A_Types. I've shown one
attribute
| > | each, but there are lots of them for each type!
| > |
| > | what I'd like to do is load this data into a few tables with
| > Master-Detail
| > | Releations. So Table A would contain a row for each A_Type plus it's
| > | attributes (with the Attribute Name as the Column Header). For a given
| > row
| > | selected in Table A, Table B would show each of the B_Types and it's
| > | attributes, etc.
| > |
| > | So, is there an easy way to get the data from the file using an
| > | XMLTextReader or some other nifty XML tool, or is this going to be a
| > brute
| > | force method with regular expressions and do loops?
| > |
| > | I'm not sure if I've supplied enough info, so if you need to know
| > something
| > | else before you can give me an answer, please let me know!! If it's
| > going
| > to
| > | be brute force, that's fine, but I'd hate to spend a few days coding,
| > only
| > | later to find out that there was a quick way to do this!
| > |
| > | TIA
| > | Lee
| > |
| > |
| >
| >
|
|