This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Changing values of parameters from xml file..


Grettir,

>Hi all.  I´m trying to get values from a xml document and put it as a ID in
>a xsl document which is meant to transform the xml document to html.  The
>problem is I don´t know how to get the value from the xml document and
>concatenate it with a string ( one character )...

You can use the concat() function within XPath to concatenate strings
together (see http://www.w3.org/TR/xpath#function-concat).  There is no
need to define functions to do it specially.  I could leave it at that, but
there are a few funny things about your stylesheet so I'll go through it
bit by bit to make sure you're not confused.

Firstly, the namespace you declare on your xsl:stylesheet element indicates
that you're using an old version of XSLT.  You should be using the
namespace "http://www.w3.org/1999/XSL/Transform".  Make sure that the XSLT
processor you're using is up to date as well.

Secondly, there's no such thing as an xsl:script element.  It's either
something from an old version of the XSLT spec or it's a proprietry
extension, in which case you shouldn't use the 'xsl' namespace for it.

Thirdly, you cannot use the syntax that you're using for making comments in
your XML stylesheet.  Probably you just added the comments like that when
you wrote your message, but just in case, comments should be inserted in
XML comments like <!-- comment text -->.  Otherwise you will get your
comments included in the text of your output.

Fourthly, the syntax $name is used to insert the value of a variable or
parameter in XSL.  Variables are set using the xsl:variable element and
parameters using the xsl:param element.  In your case, I would use
variables to define the prefixes that you want to use for parents and
children, something like:

  <xsl:variable name="parentID-prefix">p</xsl:variable>
  <xsl:variable name="childID-prefix">c</xsl:variable>

You could then use $parentID-prefix or $childID-prefix to insert the 'p'
and 'c'.  Defining them as global variables (as 'top-level elements',
direct children of xsl:stylesheet) means you can change them easily in the
future, especially if you're using them in lots of different places within
your templates.

Finally, you can't have elements within attribute values, as you've done with:

  <a class="Level1" id="$parentID(<xsl:value-of select="Kennitala"/>)">
    <xsl:value-of select="Nafn"/>
  </a>

and

  <div style="display:none" id="$childID(<xsl:value-of select="Kennitala"/>)">

Whenever you find yourself wanting to use xsl:value-of within an attribute
value, you should use 'attribute-value templates' instead.  Using
attribute-value templates means putting whatever you want to be interpreted
as an XPath within curly brackets ({}s), so basically putting whatever
you've given as the value of the 'select' attribute in curly brackets.

Putting those comments together, here is a working (in SAXON) version of
your template:

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

<xsl:variable name="parentID-prefix">p</xsl:variable>
<xsl:variable name="childID-prefix">c</xsl:variable>

<xsl:template match ="/" >
  <html>
    <body style="font-family: verdana; font-size: 7pt">
      <div id="menu" style="position: absolute;
                            width: 300px;
                            height: 200px;
                            z-index: 11;
                            visibility: visable;
                            cursor: hand;">
        <table>
          <td bgcolor="#ACF0FF" width="100%">
            <xsl:for-each select="Results/Person">
              <a class="Level1" id="{concat($parentID-prefix, Kennitala)}">
                <xsl:value-of select="Nafn"/>
              </a>
              <div style="display:none"
                   id="{concat($childID-prefix, Kennitala)}">
                <xsl:value-of select="Heimili"/>
                <xsl:value-of select="Postfang"/>
                <xsl:value-of select="Borg"/>
              </div>
             </xsl:for-each>
           </td>
         </table>
       </div>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

I should also say that it is not legal in HTML to have a 'td' element as a
direct child of a 'table' - 'td' elements need to go in rows ('tr'
elements).  In addition, you have not put anything in your HTML about what
should happen when you click on the value in the 'Nafn' element.  These
things don't detract from the well-formedness of functionality of your
stylesheet, though, just the validity and functionality of the HTML that it
produces - it may well be worth creating an HTML file that does what you
want it to do before trying to create an XSLT stylesheet that converts your
XML into that HTML.

I hope that helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 • Fax 0115 9061304 • Email
jeni.tennison@epistemics.co.uk



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]