[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Problem using variables with xpath

Subject: Problem using variables with xpath
From: Raghavan <raghavan479@xxxxxxxxx>
Date: Thu, 5 Apr 2007 09:40:29 -0700 (PDT)
 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/Document1.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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.