Construct a href= using XSLT
Asked Answered
N

6

6

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?

Nitz answered 21/9, 2009 at 12:46 Comment(1)
Without knowing the structure of the XML file it would be difficult to answer.Airspace
T
1

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
Transect answered 23/9, 2022 at 8:51 Comment(0)
N
9

Think I might have answered it myself:

<xsl:variable name="username" select="Username"/>
<a href="{$username}">Login</a>
Nitz answered 21/9, 2009 at 12:49 Comment(0)
S
4

What's wrong with using xsl:attribute?

<a><xsl:attribute name='href' select='Username' />Login</a>
Saviour answered 21/9, 2009 at 12:51 Comment(3)
To be nitpicky: The missing URL encoding is wrong. A decent URL-encoding function is missing in XSLT 1.0, when taking into account that creating HTML output was one of the design goals.Rattan
What's wrong with using xsl:attribute? select is not a valid attribute of xsl:attribute.Inapprehensible
And it took you four years to discover that error? ;-)Saviour
D
2

The same

<a href="domain.com?={$user}">OMG!</a>
Defendant answered 21/9, 2009 at 12:49 Comment(0)
T
1

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
Transect answered 23/9, 2022 at 8:51 Comment(0)
S
0

Just to remark that if you need an ampersand character as part of the URL to send multiple values, you can use "&"

<a href="ESMData.aspx?dni={DNI}&amp;consulta=1">Ficha Técnica</a>
Stalkinghorse answered 24/10, 2011 at 20:41 Comment(0)
D
-1

xsl:attribute does work:

<a><xsl:attribute name='href'><xsl:value-of select='Username'/></xsl:attribute>Login</a>
Danndanna answered 18/3, 2017 at 0:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.