Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT 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
 
Old October 10th, 2007, 01:42 AM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple XSLT Problem (HELP PLEASE)

Hello everyone,

I'm fairly new to XML and XSLT, and am trying to use it's power as a web designer/developer. I have three files:

- markup.xml
- logic.xsl
- data.xml

I am trying to make the logic recognize every instance of the "resource" tag in the markup.xml. Once it does this it then uses the information to grab data from the data.xml file and output it to the viewer.

Here is the markup.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="logic.xsl"?>

<page>
    <resource idc="page.content"/>
</page>
And here is the data.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>

<resources>
    <res id="page.content">It Works!</res>
</resources>
I don't need to tell you both of these files are simple and straight forward. Now let's take a look at the logic.xslt:

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="data" select="document('data.xml')/resources/res"/>

    <xsl:template match="page">
        <html>
            <head>
                <title>The Page</title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="resource">
        <xsl:param name="identity"><xsl:value-of select="@idc"/></xsl:param>
        <xsl:apply-templates select="concat($data,'[@id=&apos;',$identity,'&apos;]')"/>
    </xsl:template>

</xsl:stylesheet>
The logic.xsl first defines a variable needed to know where to look for the data every time the $data variable is used. Next it outputs the standard XHTML markup with a template. Near the bottom it matches all <resource> tags. This is where the magic is suppose to happen. In theory it would grab all the data, depending on the value of the <resource> attribute, from the selected element of the data.xml.

Again I am new to this so perhaps it's an obvious mistake I do not see. I am suspecting perhaps you can have document(concat()) but not concat(document()). If this is so please let me know.

Thank you so much for any and all help you can provide.
 
Old October 10th, 2007, 02:14 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The expression used in apply-templates select="" must return a set of nodes; but the concat() function returns a string. The string that it returns is

It works![@id='',$identity,'']

which doesn't seem very useful.

You seem to be trying to construct an XPath expression on the fly, which is not something XSLT allows. (There are extensions to do it, e.g. saxon:evaluate(), but it's not clear that you need them here).

I think you're making it far too complicated: you just want

select="$data[@id=$identity]"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 10th, 2007, 10:27 AM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you! Thank you thank you thank you! It does indeed work. I've been trying to wrestle with this one around in my head for a while. My problem was just as you guessed it: I was making things entirely too complicated.

I owe you one. :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
simple XSLT question _thinking XSLT 4 January 24th, 2006 10:41 AM
Problem applying simple XSLT to XML to create CSV gregclark XSLT 2 August 25th, 2005 07:30 AM
simple xml / xslt example needed badgolfer ASP.NET 1.0 and 1.1 Basics 2 January 21st, 2005 02:10 AM
XSLT simple parsing problem misu XSLT 3 August 18th, 2004 02:00 AM
Simple question about XSLT bertcox XSLT 2 May 5th, 2004 09:31 AM





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