Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Matching attribute names to element names in a different path

Reply
Thread Tools

Matching attribute names to element names in a different path

 
 
Carl
Guest
Posts: n/a
 
      04-01-2004
I want to create a generic xslt that would take xml input like:

################################################## ########################
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="lookup.xslt"?>
<root>
<Schema name="Schema1" xmlns="urn:schemas-microsoft-comml-data"
xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="lookup" content="empty" model="closed">
<AttributeType name="col1" dt:type="i2"/>
<AttributeType name="col2" dt:type="i2"/>
<AttributeType name="col3" dt:type="i2"/>
<AttributeType name="col4 dt:type="i2"/>
<AttributeType name="col5 dt:type="i2"/>
<attribute type="col1"/>
<attribute type="col2"/>
<attribute type="col3"/>
<attribute type="col4/>
<attribute type="col5>
</ElementType>
</Schema>
<lookup xmlns="x-schema:#Schema1" col1="a" col2="b" col3="c"/>
<lookup xmlns="x-schema:#Schema1" col1="d" col3="e" />
</root>
################################################## ########################

and turn it into a table with col1, col2, col3, col4 and col5 as
headings
and with the data it will put "a" in col1, "b" in col2 etc.

The problem is that, because column 4 has no data in the elements,
that column is not generated in the table.

So I am using the Schema to generate the column headings, but now I
have to match the data to the correct columns... I can onnly do that
by matching the attribute names to the corresponding element name in
the schema declaration. But I don't know how to do this...

Here is my attempt. Any help would be greatly appreciated. Thanks in
advance.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="x-schema:#Schema1"
xmlns:schema="urn:schemas-microsoft-comml-data">
<xsl:template match="/root">
<TABLE>
<xsl:apply-templates select="schema:Schema" /> <!-- headings -->
<xsl:apply-templates select="data:lookup" /> <!-- data -->
</TABLE>
</xsl:template>

<xsl:template match="schema:Schema">
<tr style="color:White;background-color:Blue;">
<td></td>
<xsl:for-each select="*/*"><!-- Select all the elements of the
schema-->
<xsl:if test="name(.)='AttributeType' and position() != 1"><!--
only pick the AttributeType elements, otherwise you will have the
columns twice -->
<xsl:for-each select="@name">
<td>
<xsl:attribute name="name"><xsl:value-of
select="name()"/></xsl:attribute>
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
<td></td>
</tr>
</xsl:template>

<!-- lookup Template -->
<xsl:template match="data:lookup">
<tr>
<td></td>
<xsl:for-each select="@*">
<td>
<xsl:value-of select="." />
</td>
</xsl:for-each>
<td></td>
</tr>
</xsl:template>
</xsl:stylesheet>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find next element in multimap not matching current element Key, How to? Victor Bazarov C++ 15 08-16-2009 08:26 PM
adding new parent element by matching child attribute value madmaler@googlemail.com XML 1 02-19-2008 11:37 AM
Generating new element names from old element names UpgradeMyBrain@gmail.com XML 2 10-20-2006 08:24 AM
Remove parent element with a child element matching a given rule patrizio.trinchini@googlemail.com XML 4 08-22-2006 11:31 AM
Validation (XHTML 1.0 Transitional): Attribute 'leftmargin' is not a valid attribute of element 'body'. anonymous ASP .Net 1 08-02-2006 08:05 AM



Advertisments