Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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?
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,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
  #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}
  #4 (permalink)  
Old February 22nd, 2016, 07:42 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
  #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,


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 05:39 PM
Displaying XML element in HTML attribute using XSLT bribon XSLT 3 June 16th, 2009 12:30 PM
need html attribute based on xml attributes charles95621 XSLT 1 May 24th, 2007 05: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.