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!

Build 'a href' link from XML using XSL

Status
Not open for further replies.

MaKSiNG

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

<record>
<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>
</record>

Bearing in mind there may be a number of these @name='c_link' groupings. The XSL I have is:

<xsl:for-each select="record/item/value/item">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="@name='link_linktarget'"/>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="target">
<xsl:choose>
<xsl:when test="@name='link_linktype'"/>
</xsl:choose>
</xsl:attribute>
</xsl:element>
</xsl:for-each>

Is this anywhere near correct as a start? How do I incorporate the @name='link_linktext' as the displayed clickable link?


Thx,
MaKS
 
On the xml you posted:
Code:
<xsl:for-each select="record/item[@name='c_link']">
  <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>
</xsl:for-each>

However, you could make it a lot simpeler like this:
Code:
<record>
   <item name="c_link">
      <linktext>Details</linktext>
      <linktarget>[URL unfurl="true"]http://www.tek-tips.com/</linktarget>[/URL]
      <linktype>_self</linktype>
   </item>
</record>

<xsl:for-each select="record/item[@name='c_link']">
  <xsl:element name="a">

    <xsl:attribute name="href">
      <xsl:value-of select="linktarget" />
    </xsl:attribute>
    
    <xsl:attribute name="target">
      <xsl:value-of select="linktype" />
    </xsl:attribute>

    <xsl:value-of select="linktext" />

  </xsl:element>
</xsl:for-each>
 
jel,

That works a treat!
Thanks for that.

MaKS

Thx,
MaKS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top