Construct a href= using XSLT

xslt

Solution

Using the Oracle XE sample database I wanted to produce a table of customers where - if the customer have one or more contacts or orders - I wanted a link to a list of those, passing the CUSTOMER_ID as a GET (query_string) variable. Here's how...

Here's a sample of the XML, obtained from Oracle's DBMS_XMLGEN.getXML function...

<?xml version="1.0" encoding="UTF-8"?>
<ROWSET>
 <ROW>
  <CUSTOMER_ID>177</CUSTOMER_ID>
  <NAME>United Continental Holdings</NAME>
  <ADDRESS>2904 S Salina St, Syracuse, NY</ADDRESS>
  <WEBSITE>http://www.unitedcontinentalholdings.com</WEBSITE>
  <CREDIT_LIMIT>5000</CREDIT_LIMIT>
  <ORDERS>0</ORDERS>
  <CONTACTS>1</CONTACTS>
 </ROW>
 <ROW>
  <CUSTOMER_ID>180</CUSTOMER_ID>
  <NAME>INTL FCStone</NAME>
  <ADDRESS>5344 Haverford Ave, Philadelphia, PA</ADDRESS>
  <WEBSITE>http://www.intlfcstone.com</WEBSITE>
  <CREDIT_LIMIT>5000</CREDIT_LIMIT>
  <ORDERS>2</ORDERS>
  <CONTACTS>1</CONTACTS>
 </ROW>
</ROWSET>

Here's the style sheet...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <table border="1">
    <xsl:for-each select="ROWSET/ROW">
    <tr>
       <xsl:apply-templates/>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>

<!-- these nodes have specific behaviour -->

<xsl:template match="ORDERS">
<td>
<xsl:if test=". &gt; 0">
    <!-- here's the anchor tag -->
    <a href="/Orders?CUSTOMER_ID={../CUSTOMER_ID}"><xsl:value-of select="."/></a>
</xsl:if>
</td>
</xsl:template>

<xsl:template match="CONTACTS">
<td>
<xsl:if test=". &gt; 0">
    <a href="/Contacts?CUSTOMER_ID={../CUSTOMER_ID}"><xsl:value-of select="."/></a>
</xsl:if>
</td>
</xsl:template>

<!-- all other node are just displayed as a cell -->

<xsl:template match="*">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>

Rendered as HTML, the anchors look like this...

http://localhost:8080/Orders?CUSTOMER_ID=184

Problem

I want to create this: ``` <a href="domain.com?=USERNAME">Login</a> ``` where USERNAME = in XML so the HTML output is specific to the user currently logged in. Can anyone advise? I know I can use: ``` <xsl:variable name="class" select="a:Subject"/> <p class="{$class}">English</p> ``` To extract a value and use it as a CSS Class but what about using it for a link?

Original source