Subject: RE: Problem using variables with xpath
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 5 Apr 2007 18:52:45 +0100
|
>the same piece of XSL is unable to do that string compare
what are the symptoms? Does it say "I am unable to do this string compare",
or what?
> <xsl:variable name="pdfdoctitle">
> <xsl:value-of select="//Document/@url"/>
> </xsl:variable>
Don't do this, it's very inefficient and sometimes means something different
from what you intended.
Do:
<xsl:variable name="pdfdoctitle" select="//Document/@url"/>
Michael Kay
http://www.saxonica.com/
> -----Original Message-----
> From: Raghavan [mailto:raghavan479@xxxxxxxxx]
> Sent: 05 April 2007 17:40
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Problem using variables with xpath
>
> Apologize for the long mail
>
> Am quite new to XSL and I am running into a strange problem.
>
> I have 2 xml files
> 1) input.xml 2) metadata.xml
> The xsl is applied on input.xml. The xsl extracts the data
> corresponding to "url" attribute. This value is looked up
> (using xpath) on the second xml
> (metadata.xml) and extract the necessary values from metadaat.xml
>
> I've attached the necessary source files. I constructed the
> xsl using Stylus Studio (also tried
> XMLspy) and xsl works as expected. Then I have this simple
> Java code that performs XSL transformation and the same piece
> of XSL is unable to do that string compare
>
> Note:
> During my unit testing
> I replaced the following line with a static text and the
> transformation worked as expected original:<xsl:for-each
> select="$doc2node//doc[@id=$pdfdoctitle]">
> modified:<xsl:for-each
> select="$doc2node//doc[@id='http://www.something.com/path1/Doc
> ument1.pdf']">
>
> Since am new to XSL, am not sure if we can use variables in
> XPath in first place!!!
>
> Thanks,
> -Raghav
>
> --------------------------------------------------------------
> ---------------------------
> Input XML
> --------------------------------------------------------------
> ---------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <Content>
> <Document Type="Web"
> url="http://www.something.com/path1/Document1.pdf"/>
> </Content>
>
> --------------------------------------------------------------
> ---------------------------
> My XSL
> --------------------------------------------------------------
> ---------------------------
> <?xml version='1.0' encoding='utf-8'?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="html"/>
> <xsl:template match="/Content">
>
> <xsl:variable name="pdfdoctitle">
> <xsl:value-of select="//Document/@url"/>
> </xsl:variable>
>
> <xsl:variable name="doc2node"
> select="document('metadata.xml')"/>
> <html><head></head><body>
> <p>
> <!-- String compare scenatio 1
> -->
> <rr>
> <xsl:for-each select="$doc2node//doc">
> <xsl:if test="@id=$pdfdoctitle">
> <xsl:value-of
> select="@title" />
> </xsl:if>
> </xsl:for-each>
> </rr>
> <!-- String compare scenatio 2
> -->
> <rr2>
> <xsl:for-each
> select="$doc2node//doc[@id=$pdfdoctitle]">
> <xsl:value-of select="@title"/>
> </xsl:for-each>
> <rr2>
> <!-- Display value of pdfdoctitle -->
> <rr3>
> <xsl:value-of select="$pdfdoctitle"/>
> </rr3>
>
> </p>
> </body></html>
> </xsl:template>
> </xsl:stylesheet>
>
> --------------------------------------------------------------
> ---------------------------
> metadata.xml file used in the XSL
> --------------------------------------------------------------
> ---------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <documents>
> <doc id="x" title="title for document x"
> issuedate="12-Aug-2005" issuenumber="1" />
> <doc id="z" title="This is document z title"
> issuedate="1-Jan-2006" issuenumber="2" />
> <doc id="y" title="This is document y title"
> issuedate="10-Aug-2006" issuenumber="2" />
> <doc
> id="http://www.something.com/path1/Document1.pdf"
> title="Title for something document"
> issuedate="10-Aug-2005" issuenumber="22" /> </documents>
>
> --------------------------------------------------------------
> ---------------------------
> Transform.java
> --------------------------------------------------------------
> ---------------------------
> import javax.xml.transform.Source;
> import javax.xml.transform.Transformer;
> import javax.xml.transform.TransformerFactory;
> import javax.xml.transform.stream.StreamSource;
> import javax.xml.transform.stream.StreamResult;
> import java.io.*;
>
> public class Transform {
>
> /**
> * Performs an XSLT transformation, sending the results
> * to System.out.
> */
> public static void main(String[] args) throws Exception {
> if (args.length != 2) {
> System.err.println(
> "Usage: java Transform [xmlfile] [xsltfile]");
> System.exit(1);
> }
>
> File xmlFile = new File(args[0]);
> File xsltFile = new File(args[1]);
>
> // JAXP reads data using the Source interface
> Source xmlSource = new StreamSource(xmlFile);
> Source xsltSource = new
> StreamSource(xsltFile);
>
> // the factory pattern supports different XSLT processors
> TransformerFactory transFact =
> TransformerFactory.newInstance();
> Transformer trans =
> transFact.newTransformer(xsltSource);
>
> trans.transform(xmlSource, new
> StreamResult(System.out));
> }
> }
>
>
>
> ______________________________________________________________
> ______________________
> Don't get soaked. Take a quick peek at the forecast with the
> Yahoo! Search weather shortcut.
> http://tools.search.yahoo.com/shortcuts/#loc_weather
|