Forum Moderators: open

Message Too Old, No Replies

Help w/ XSL

         

tjhorne

9:41 pm on Nov 29, 2005 (gmt 0)

10+ Year Member



I'm trying to style the following XML file, but I am having trouble, any ideas how to get me started?

Sample:

<Alert Document_Name="Alerts" Document_Description="" Document_Version="1" Document_Time="29&#47;11&#47;2005 14:27:42" Document_Generator="c:&#92;documents and settings&#92;thorne&#92;local settings&#92;application data&#92;assembly&#92;dl2&#92;9r6xkwdd.lqd&#92;j8dpb2dv.9en&#92;79d53564&#92;00a83434&#95;14afc501&#92;psvdclient.dll">
<Item Kind="Alert" Title="Measures Missing Data">
<From>
<User Name="C: THorne" Email="THorne@example.com"/>
</From>
<To>
<User Name="C: THorne" Email="THorne@example.com"/>
</To>
<Header><![CDATA[The measures listed below require data entry.]]>
</Header>
<Item Kind="Section" Title="P: [AE] Selected Meas">
<Item Kind="Comparison" Title="Actual vs. Target"/>
<Item Kind="Period" Title="2005/10"/>
<Item Kind="Measure (Location)" Title="P: 05 # Readership Initiatives (Rockingham)">
<Item Kind="Index" Title="--"/>
</Item>
<Item Kind="Measure (Location)" Title="P: # Pg A1 Community Issues that Drive Rdrshp (Rockingham)">
<Item Kind="Index" Title="--"/>
</Item>
<Item Kind="Measure (Location)" Title="P: 05 # Error Reduction Processes (Rockingham)">
<Item Kind="Index" Title="--"/>
</Item>
<Item Kind="Measure (Location)" Title="P: 05 # Content Diversity Strategies (news) (Rockingham)">
<Item Kind="Index" Title="--"/>
</Item>
<Item Kind="Measure (Location)" Title="P: # Promo References (Rockingham)">
<Item Kind="Index" Title="--"/>
</Item>
<Item Kind="Measure (Location)" Title="P: % Last Page Deadline (Rockingham)">
<Item Kind="Index" Title="--"/>
</Item>
</Item>
</Item>
</Alert>

[edited by: jatar_k at 7:02 am (utc) on Dec. 1, 2005]
[edit reason] generalized email [/edit]

choster

5:22 pm on Nov 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you be more specific? You don't use XSL to style in the same way you use CSS for instance.

tjhorne

7:56 pm on Nov 30, 2005 (gmt 0)

10+ Year Member



I have gotten a little ways with my XSL document, but basically I'm trying to pull the information from my XML file and pull the "title" of each <Item> out and put it in its own row of an HTML table. This is what I have so far but it throws all of my "title" information from my XML file into one cell.

Here is my XML File:

<Alert Document_Name="Alerts">
<Item Kind="Alert" Title="Measures Missing Data">
<From>
<User Name="C: THorne" Email="THorne@example.com"/>
</From>
<To>
<User Name="C: THorne" Email="THorne@example.com"/>
</To>
<Header><![CDATA[The measures listed below require data entry.]]>
</Header>
<Item Kind="Section" Title="P: [AE] Selected Meas">
<Item Kind="Comparison" Title="Actual vs. Target"/>
<Item Kind="Period" Title="2005/10"/>
</Item>
</Item>
</Alert>

Here is my XSL file:

<xsl:for-each select="Alert">
<xsl:for-each select="Item">
<table border="2" height="#" table-layout="auto" width="#%">
<tr>
<td colspan="9">
<xsl:for-each select="@Title">
<span style="word-wrap: normal; font-weight: bold; font-family: verdana;">
<xsl:value-of select="." />
</span>
</xsl:for-each>
</td>
</tr>
<tr bgcolor="silver">
<td>Object Type</td>
<td>Name</td>
<td>Name</td>
<td>Name</td>
<td>Name</td>
</tr>
<xsl:for-each select="Item">
<tr>
<td>
<xsl:for-each select="@Kind">
<span style="word-wrap:normal; ">
<xsl:value-of select="." />
</span>
</xsl:for-each>
</td>
<td>
<xsl:for-each select="@Title">
<span style="word-wrap:normal; ">
<xsl:value-of select="." />
</span>
</xsl:for-each>
</td>
<td>
<xsl:for-each select="Item">
<xsl:for-each select="@Title">
<span style="word-wrap:normal; ">
<xsl:value-of select="." />
</span>
</xsl:for-each>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>

</table>
</xsl:for-each>
</xsl:for-each>

Any help would be greatly appreciated.

[edited by: jatar_k at 7:02 am (utc) on Dec. 1, 2005]
[edit reason] generalized email [/edit]

choster

6:36 pm on Dec 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, thanks for your code. For starters, you should know the <xsl:for-each>s on attributes are superfluous-- by definition, attributes on an element must be unique (i.e. <Item Title="xyz" Title="abc"> would be invalid XML).

It's still a little hard to understand what you want, since <Item> appears at three different levels in the source. Incidentally your display may also be affected by the conflicting output code-- your specify colspan="9", then only provide five heading cells, then only output three actual data points.

But let's assume that you literally want everything inlined into a two-column HTML <table> for each <Alert>, using the @Title of the immediate child <Item> as a heading for the whole table regardless of its @Kind; discard the <Header>, <From> and <To> blocks; and display the @Kind and @Title of each descendant <Item> side by side. I'd try something like this:

<xsl:template match="Alert/Item">
<table>
<tr>
<th colspan="2">
<!-- outputs Alert/Item/@Title -->
<xsl:value-of select="@Title" />
</th>
</tr>

<!-- process the immediate child Item, which comprises the table data. -->
<xsl:apply-templates select="Item" />

</table>
</xsl:template>

<!-- process on anything named Item at any level -->
<xsl:template match="Item">
<tr>
<td><xsl:value-of select="@Kind" /></td><td><xsl:value-of select="@Title" /></td>
</tr>
<!-- we instruct the processor to do any child Items of the current Item -->
<xsl:apply-templates />
</xsl:template>

Using MSXML this yields

+-------------------------------------------------------------------+
¦ Measures Missing Data ¦
+---------------------+---------------------------------------------+
¦ Section ¦ P: [AE] Selected Meas ¦
+---------------------+---------------------------------------------+
¦ Comparison ¦ Actual vs. Target ¦
+---------------------+---------------------------------------------+
¦ Period ¦ 2005/10 ¦
+---------------------+---------------------------------------------+
¦ Measure (Location) ¦ P: 05 # Readership Initiatives (Rockingham) ¦
+---------------------+---------------------------------------------+
¦ Index ¦ -- ¦
+---------------------+---------------------------------------------+

etc

+---------------------+---------------------------------------------+
¦ Measure (Location) ¦ P: % Last Page Deadline (Rockingham) ¦
+---------------------+---------------------------------------------+
¦ Index ¦ -- ¦
+---------------------+---------------------------------------------+

tjhorne

9:21 pm on Dec 1, 2005 (gmt 0)

10+ Year Member



Thanks for your help you've got me on the right track. One more question, with what you've suggested, would their be a way to exclude lets say the "index" description and its value?

Again, thanks for your help!

choster

5:44 pm on Dec 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In my first example, we do an <xsl:apply-templates /> inside the second template, which tells the processor "start over from the beginning, but this time with all the children of the current node instead of the root of the document." We can add an XPath predicate to this line to tell it to start over, but only apply the templates to certain children:

<xsl:apply-templates select="Item[@Kind!='Index']" />

Thus the second template will not be applied to any <Item> whose Kind attribute has the value "Index," meaning the row will not be output.

tjhorne

4:55 pm on Dec 6, 2005 (gmt 0)

10+ Year Member



I cannot get this to work, I replaced the following line:

<xsl:apply-templates select="Item" />

with:

<xsl:apply-templates select="Item[@Kind!='Index']" />

Could it be something to do with that the line that has the values for index are wrapped like so:

<Item Kind="Section" Title="C: [TH] Missing Data - Tampa">
<Item Kind="Comparison" Title="Actual vs. Target"/>
<Item Kind="Period" Title="2005/10"/>
<Item Kind="Measure (Location)" Title="P: 05 # Community Relations Initiatives (Tampa)">
<Item Kind="Index" Title="--"/>
</Item>
</Item>

I've gotten a long ways thanks to your help, here is what I have come up with so far:

<snip>

I'd just like to get the **No Measures Missing Data** line to display within the body of the table.

Thanks again!

[edited by: jatar_k at 3:52 am (utc) on Dec. 7, 2005]
[edit reason] removed url [/edit]