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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference a further child Element from within 'for-each' select 1

Status
Not open for further replies.

MaKSiNG

Technical User
Dec 4, 2003
19
I have an XML file as follows:

<record>
<item name="linksheading">
<value>Design</value>
</item>
<item name="linkspara">
<value>Design Consultancy</value>
</item>
<item name="r_links">
<value>
<item name="c_link">
<value>
<item name="link_linktext">
<value>Details</value>
</item>
</value>
</item>
</value>
</item>
</record>

Via an XSL I hope to achieve a HTML output which prints a table with the 3 value in different table rows beneath each other. Eg:

Design
Design Consultancy
Details

I have the start of the XSL which follows but am struggling to reference the value of the "@name='link_linktext'" (Details) after having iterated through "@name='linksheading'" and "@name='linkspara'" since this is a child node 4 levels down from the context I am in currently.

<table>
<xsl:for-each select="record/item">
<tr>
<xsl:choose>
<xsl:when test="@name='linksheading'">
<td>
<xsl:value-of select="value" />
</td>
</xsl:when>
</xsl:choose>
</tr>
<tr>
<xsl:choose>
<xsl:when test="@name='linkspara'">
<td>
<xsl:value-of select="value" />
</td>
</xsl:when>
</xsl:choose>
</tr>

<tr>
<xsl:choose>
<xsl:when test="@name='link_linktext'">
<td>
<xsl:value-of select="value" />
</td>
</xsl:when>
</xsl:choose>
</tr>

The bolded 'when test' obviously looks for the @name in the wrong context and I have tried inserting a new 'for-each' select for this <tr> but that causes this @name to be printed after the first 2 test have iterated through all of their satisfying nodes.

Please help,

Thx,
MaKS
 
If I understand you correctly, you want to display the content from each value-node that does't have any subnodes?
Code:
<table>
  <xsl:for-each select="record//value[not(*)]">
    <tr>
      <td>
        <xsl:value-of select="."/>
      </td>
    </tr>
  </xsl:for-each>
</table>
 
jel,

Not quite.
This relates to the hyperlink question that you helped me with.
What I have is the following:

<record>
<item name="linksheading">
<value>Design</value>
</item>
<item name="linkspara">
<value>Design Consultancy</value>
</item>
<item name="r_links">
<value>
<item name="c_link">
<value>
<item name="link_linktext">
<value>Details</value>
</item>
<item name="link_linktarget">
<value> </value>
</item>
<item name="link_linktype">
<value>_self</value>
</item>
</value>
</item>
</value>
</item>
</record>

I need the values to be displayed in a list. If they were all in the same context, ie. record/item, it wouldn't be a problem, but the hyperlinks are under record/item/value/item.
I guess what I need to do is a type of nested <xsl:for-each select> but am not sure if this is possible or how this works.

What I have so far is:

<table>
<xsl:for-each select="record/item">
<tr>
<xsl:choose>
<xsl:when test="@name='linksheading'">
<td>
<xsl:value-of select="value" />
</td>
</xsl:when>
</xsl:choose>
</tr>
<tr>
<xsl:choose>
<xsl:when test="@name='linkspara'">
<td>
<xsl:value-of select="value" />
</td>
</xsl:when>
</xsl:choose>
</tr>

<table>
<xsl:for-each select="record/item/value/item[@name='c_link']">
<tr>
<td>
<xsl:element name="a">
<xsl:attribute="href">
<xsl:value-of select="value/item[@name='link_linktarget]'/value" />
</xsl:attribute>
<xsl:attribute="target">
<xsl:value-of select="value/item[@name='link_linktype]'/value" />
</xsl:attribute>
<xsl:value-of select="value/item[@name='link_linktext]'/value" />
</xsl:element>
</td>
</tr>
</xsl:for-each>
</table>

</table>

It is the highlighted section which I am unsure about as this doesn't seem to work when nested like this. The 2 individual parts (for the separate context) work when executed independantly.

What I would like my html page to look like is simply:

Design
Design Consultancy
Details (this being a hyperlink to www.tek-tips.com)


Thx,
MaKS
 
No sweat:
Code:
<table>
  <xsl:for-each select="record">
    <tr>
      <td>
        <xsl:value-of select="item[@name='linksheading']/value" />
      </td>
    </tr>
    <tr>
      <td>
        <xsl:value-of select="item[@name='linkspara']/value" />
      </td>
    </tr>
<!-- Bearing in mind there may be a number of these @name='c_link' groupings -->
    <xsl:for-each select="item/value/item[@name='c_link']">
      <tr>
        <td>
          <xsl:element name="a">
            <xsl:attribute name="href">
              <xsl:value-of select="value/item[@name='link_linktarget']/value" />
            </xsl:attribute>
            <xsl:attribute name="target">
              <xsl:value-of select="value/item[@name='link_linktype']/value" />
            </xsl:attribute>
            <xsl:value-of select="value/item[@name='link_linktext']/value" />
          </xsl:element>
        </td>
      </tr>
    </xsl:for-each>
  </xsl:for-each>
</table>
 
Great!
Thanks again jel.


Thx,
MaKS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top