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!

Reusing an attribute field for multiple columsn in one XML file

Status
Not open for further replies.

hoppers69

Programmer
Apr 13, 2006
2
US
Hi there,
I am somewhat new to XML and working on using XML in some data intergrations. I have gotten an XML file and the attribute column reuses iteself over and over for the column names. I think they did this since I will specify it once in the DTD and then someone can add or remove columns all they want without re-creating the DTD.

What I want to know, is how to parse out each column of data associated with the top level attribute (in this case a part #). Then about 80 columns underneath it, are the columns associated to that part number and then it ends and repeats itself for a new part # etc.

I have my example below (you can see the FEATURE attributekey keeps repeating itself with a new column name). I basically want to pull out each attributekey and make that the column header and take the value and put it in the same row below the column header to make one full record.

Is this a common way to use XML? Seems quite messy.


FEATURE attributekey="PArt_Number" changedby="admin" changedat="20050503195200">
<VALUE>08586241</VALUE>
</FEATURE>
<FEATURE attributekey="Name" lang="ENS" sourcelang="ENS" changedby="admin" changedat="20050503195200">
<VALUE>A1 Project Development</VALUE>
</FEATURE>
</FEATURE>
<FEATURE attributekey="Active_Sales" changedby="admin" changedat="20050503195200">
<VALUE>Y</VALUE>
</FEATURE>
<FEATURE attributekey="Active_Spare" changedby="admin" changedat="20050503195200">
<VALUE>N</VALUE>
</FEATURE>
<FEATURE attributekey="Buyer_Status" changedby="admin" changedat="20050503195200">
<VALUE>A</VALUE>
</FEATURE>
<FEATURE attributekey="Additional_Text" lang="ENS" sourcelang="ENS" changedby="admin" changedat="20050602094300">
<VALUE>Test Text</VALUE>
</FEATURE>
 
>...Then about 80 columns underneath it...
>...the FEATURE attributekey keeps repeating itself with a new column name...
>...pull out each attributekey and make that the column header...
What does it mean here, "column"?
 
Well by column I mean that each one of these:

FEATURE attributekey="Part_Number"
FEATURE attributekey="Active_Sales"
FEATURE attributekey="Buyer_Status"

is a new column header I need to pull out. but they all reuse the "FEATURE attributekey" part and I have that repeated (what I showed 3 examples above) about 82 times. So basically I need to pull out each FEATURE attributekey and put it as my column header and then put the <value> such as "Y" or "A" or "1234" below the column header.

Dont know if this is a good way of using XML...Hope that makes sense. Thank you,
-Matt
 
This is a partial setup, not fully automatic and generalized. It only takes 6 attributekey headers as your sample shows. I do not automatize and fully generalize that part because I need some further thinking on the setting and some iteration templates to build the <td>&#xa0;</td>. That's no enjoyable task.
[tt]
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="html" omit-xml-declaration="yes" />
<xsl:key name="keyidx" match="FEATURE" use="@attributekey" />

<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<xsl:for-each select="root/FEATURE[generate-id() = generate-id(key('keyidx',@attributekey)[1])]">
<td><xsl:value-of select="@attributekey" /></td>
</xsl:for-each>
</tr>
<xsl:for-each select="root/FEATURE[generate-id() = generate-id(key('keyidx',@attributekey)[1])]">
<xsl:for-each select="key('keyidx',@attributekey)">
<xsl:variable name="p" select="position()" />
<xsl:choose>
<xsl:when test="$p = 1">
<tr><td><xsl:value-of select="VALUE" /></td> <td>&#xa0;</td> <td>&#xa0;</td><td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td></tr>
</xsl:when>
<xsl:when test="$p = 2">
<tr><td>&#xa0;</td> <td><xsl:value-of select="VALUE" /></td> <td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td></tr>
</xsl:when>
<xsl:when test="$p = 3">
<tr><td>&#xa0;</td> <td>&#xa0;</td> <td><xsl:value-of select="VALUE" /></td> <td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td></tr>
</xsl:when>
<xsl:when test="$p = 4">
<tr><td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td> <td><xsl:value-of select="VALUE" /></td> <td>&#xa0;</td> <td>&#xa0;</td></tr>
</xsl:when>
<xsl:when test="$p = 5">
<tr><td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td> <td><xsl:value-of select="VALUE" /></td> <td>&#xa0;</td></tr>
</xsl:when>
<xsl:when test="$p = 6">
<tr><td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td> <td>&#xa0;</td> <td><xsl:value-of select="VALUE" /></td></tr>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
[/tt]
If you find the time to automate and generalize that part on column size, please post it back.
 
Hopefully tsuji has answered the question, but I'm not sure I understand what you want.
Then about 80 columns underneath it, are the columns associated to that part number and then it ends and repeats itself for a new part # etc.
How are the columns associated? Are they <FEATURE> elements?

Its not really clear what you are trying to do. Post what you want the output to look like and a bit more information on the input.

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