Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0
This is the forum to discuss the Wrox book XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition by Michael Kay; ISBN: 9780470192740
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 22nd, 2016, 02:16 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2016
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default XML element to HTML attribute

I would like to turn an XML file with content like this:

Code:
    <Item>
    <Name>Alpha</Name>
    <URL>http://www.test.com</URL>
    </Item>

    <Item>
    <Name>Beta</Name>
    <URL></URL>
    </Item>
into an HTML file with content like this with XSLT.

Code:
    <a href="http://www.test.com">Alpha</a>

    <a>Beta</a>
I have tried any number of approaches with no success.
Any suggestions?
Reply With Quote
The Following User Says Thank You to Jim Mehl For This Useful Post:
  #2 (permalink)  
Old February 22nd, 2016, 07:00 PM
mhkay's Avatar
Wrox Author
Points: 18,274, Level: 58
Points: 18,274, Level: 58 Points: 18,274, Level: 58 Points: 18,274, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,932
Thanks: 0
Thanked 282 Times in 277 Posts
Default

Show us one thing that you tried, and show us how it failed. We need to know where you went wrong so that we can help you correct your misunderstandings.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old February 22nd, 2016, 07:21 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2016
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default XML element to HTML attribute

OK, I tried the following and a few variations.

[code}
<?xml version="1.0" encoding="iso-8859-1"?>

<!-- test.xsl -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:output method="html" encoding="iso-8859-1" />

<xsl:template match="ItemList">
<html>
<head>
<title>Items</title>
<br/>
</head>
<body>
<xsl:apply-templates select="Item"/>
</body>
</html>
</xsl:template>

<xsl:template match="Item">
<xsl:apply-templates select="Name"/>
<xsl:apply-templates select="URL"/>
</xsl:template>

<xsl:template match="Name">
<p>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@URL"/>
</xsl:attribute>
<xsl:value-of select="."/>
</a>
</p>
</xsl:template>

<xsl:template match="URL">
<xsl:value-of select="." />
</xsl:template>

</xsl:stylesheet>
[/code}
Reply With Quote
  #4 (permalink)  
Old February 22nd, 2016, 07:42 PM
mhkay's Avatar
Wrox Author
Points: 18,274, Level: 58
Points: 18,274, Level: 58 Points: 18,274, Level: 58 Points: 18,274, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,932
Thanks: 0
Thanked 282 Times in 277 Posts
Default

Let's assume that the XML you showed us has an ItemList element wrapped around it, and that there's no namespace declaration. Then you're almost there.

Generating a br element inside the HTML head element is bad HTML, but it's not bad XSLT.

This template:
Code:
<xsl:template match="Name">
<p>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@URL"/>
</xsl:attribute>
<xsl:value-of select="."/>
</a>
</p>
</xsl:template>
is almost right, but URL is an element not an attribute so you don't want that '@' sign. It's on the same level as Name, so you need to address it as <code>../URL</code>. It can then be simplified to

Code:
<xsl:template match="Name">
<p><a href="{../URL}"><xsl:value-of select="."/></a></p>
</xsl:template>
Finally, in this template:

Code:
<xsl:template match="Item">
<xsl:apply-templates select="Name"/>
<xsl:apply-templates select="URL"/>
</xsl:template>
you don't need to do anything with the URL at this level (you've handled it while processing the Name), so just cut out that second apply-templates.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #5 (permalink)  
Old February 22nd, 2016, 08:46 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2016
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default XML element to HTML attribute

Thanks Michael, that's a BIG help. I still have the the problem of eliminating the href='''' when the original URL element is null, but I suspect that's just a matter of an appropriate xsl:if in the right place.
You might want to consider including a bit more information about curly brackets in future editions,
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT output to HTML - xml element text should look exactly as laid out in xml element DjGogga XSLT 4 July 21st, 2011 06:39 PM
Displaying XML element in HTML attribute using XSLT bribon XSLT 3 June 16th, 2009 01:30 PM
need html attribute based on xml attributes charles95621 XSLT 1 May 24th, 2007 06:21 PM
value-of within the HTML element attribute BrendonMelville XSLT 1 March 14th, 2006 11:32 AM
Adding an Element with Attribute to XML file xergic Classic ASP XML 0 November 20th, 2004 08:26 AM



All times are GMT -4. The time now is 02:54 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.