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 April 22nd, 2009, 03:19 PM
Authorized User
 
Join Date: Mar 2009
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default xlink:href, xml and xslt

Hi

I'm just trying to simplify down some code and some xml applications make use of xlink:href so as not to repeat already declared data. I'm having difficulty though using this technique to output something that has been xlink'ed.

So if I had the following xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<example xmlns:xlink="http://www.w3.org/1999/xlink">
    <tier1>
        <point id="point1">10</point>
    </tier1>
    <tier2>
        <point xlink:href="#point1" />
    </tier2>
</example>
What XSLT would be required to produce the following output?
Code:
<?xml version="1.0" encoding="UTF-8"?>
10
note that the value '10' must come from 'tier2/point'.

I have tried a few things but can't seem to get the correct output! Tis baffling!

Thanks
 
Old April 22nd, 2009, 03:35 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:value-of select="/example/tier1/point[@id = substring-after(/example/tier2/point/@xlink:href, '#')]" xmlns:xlink="http://www.w3.org/1999/xlink"/>
Could of course use an xmlns:xlink declaration on the xsl:stylesheet element itself
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
jamesdurham (April 22nd, 2009)
 
Old April 22nd, 2009, 03:41 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It depends (as you hint) on what other inputs the stylesheet might encounter. For example, will a point within a tier2 element always have an xlink:href attribute, or might it contain the value directly? Could the xlink:href point to a different document, or will it always be an intra-document link?

You could start with this and generalize as necessary:

<xsl:template match="point">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>

<xsl:template match="@xlink/@href[starts-with(., '#')]">
<xsl:apply-templates select="id(substring(.,2)"/>
</xsl:template>

<xsl:template match="point/text()">
<xsl:value-of select="."/>
</xsl:template>

This assumes (in line with the semantics for XLink) that the attribute named "id" is defined as an ID in the DTD or schema, and therefore the id() function works. If not, it may be safer to use key().
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
jamesdurham (April 22nd, 2009)
 
Old April 22nd, 2009, 07:28 PM
Authorized User
 
Join Date: Mar 2009
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default

thanks again for such quick replies!

For such a simple case as I described it would be suitable to use the method that Martin proposed. However, as you mention Michael, if more rules are involved then perhaps more generalised solutions would be appropriate.

I just changed Martin's code slightly to get this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:template match="example">
    <xsl:variable name="path" select="tier1/point[@id = substring-after(/example/tier2/point/@xlink:href, '#')]" />
    <point>
        <xsl:value-of select="$path"/>
    </point>
</xsl:template>
</xsl:stylesheet>
Basically something with these principles may suffice for me for now. The scenario for the usage would be that fairly early on in the xml structure the user states key slope location coordinates (such as the toe and crest). It is then likely that these coordinates would be repeated elsewhere in the document (note only one xml document's being used), perhaps as a location for some geometric feature or such. Instead of them repeating the coordinates they could simply use xlink:href to call a previously defined set.

I've used a global variable to hold the XPath expression so that the code appears cleaner and may be more suitable for repeated cases. This method is obviously fairly restricted to knowing which coordinates could be repeated and where they would be repeated (if you follow me!). I suppose conditional statements (choose/when/otherwise) could be used with this in case href's weren't used.

I have to hand this work in in a under a week now so only got time for some minor adjustments (on top of finishing the report) so I shall come back to the more sophisticated techniques after the exams (just over a month from now).

Thanks for all of your help, really appreciate it.
 
Old April 22nd, 2009, 09:10 PM
Authorized User
 
Join Date: Mar 2009
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default XML, XSLT and web service

Hi again.. I don't know if this question strictly ties in with this area of the forum however I'm not sure how involved XSLT is with this and it does relate to how I would use xlink:href:

http://www.multimap.com/openapidocs/...n.htm#examples
That URL points to the multimap web service for coordinate transformation between various coordinate reference systems. It would be helpful to me if I could use this web service but I'm struggling to understand how the process works.

Lets say I have the following xml fragment:
Code:
<gml:Point gml:id="slopeBaseWGS84" srsName="WGS84">
     <gml:pos>54.439390 -3.294112</gml:pos>
</gml:Point>
And i want to convert those to OS grid latitude and longitude values using their web service and then to use the values they feed back.

They request for an 'xml output' of something like this:

Code:
http://developer.multimap.com/API/convert/1.2/OA09042217172872167?output=xml&system=osng&x_1=505658&y_1=316165&x_2=316165&y_2=505658
(where OA09042217172872167 is an example API key)

This gives the result in XML of:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Results xmlns="http://clients.multimap.com/API" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://clients.multimap.com/API http://clients.multimap.com/Schema/convert_1.2.xsd">
  <Point>
    <Lat>54.43940</Lat>
    <Lon>-3.29411</Lon>
  </Point>
</Results>
Which is correct. (To see this i simply entered the URL in to the browser directly.)

What I'd like to know is how I could implement this in to my process? Currently the user submits an XML file (according to my schema) and it produces (via Saxon-B) outputs such as SVG, KML and HTML pages (which all interlink to produce a 'case' page).

It would be very useful if I could squeeze set something up so that once the xml was submitted it would then, where required, translate coordinate pairs in to another coordinate reference system (CRS), return their values in to a known place within the XML (such as as a following sibling to the XML fragment shown nearer the top) so that the XSLT files could then perform translations based on the new coordinates. Maybe it works differently?

Thanks for any help
p.s. I've read up on SOAP before but don't recall this type of request and integration, but maybe I'm wrong.

Last edited by jamesdurham; April 22nd, 2009 at 09:15 PM.. Reason: added p.s
 
Old April 22nd, 2009, 10:07 PM
Authorized User
 
Join Date: Mar 2009
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Red face

Come to think of it there is one final thing that's been bugging me and that's how to actually implement an XQuery file?

I have tested this xquery against my xml file and it works. Here's the xquery
Code:
declare namespace geo = "http://www.dur.ac.uk/geo-engineering";
declare namespace gml = "http://www.opengis.net/gml";

for $x in doc("Slope.xml")/geo:slopeData/geo:case/geo:slope/geo:toe/geo:location/gml:Point[@srsName='EPSG:27700']
return $x/gml:pos
Only a simple example (I have created more advanced ones ! So this simply returns the values requested from within the file Slope.xml given that the xquery file is placed in the same directory. I have used oXygen to check it.

If a user comes on to my site and sees a HTML table listing all of the Slope.xml files added to the system (whereby the table is updated by a php script each time a new one is submitted) but wants to find all of the files that can match some XQuery expression (such as the one above) then how does he do it? I guess I need to provide some functionality for this process, in which case, what can I do to allow this? I have jQuery-powered sorting fields for my table columns but a user may wish for a more in depth ordering according to some other content from the XML files stored. (The Slope.xml files are stored in a location 'SlopeCase/inventory/Case[ID]/Slope.xml' (where [ID] is the case's ID number).

The simplest example would be to query these files with 'retrieve all of the Slope.xml files that have a slope angle of greater than 30degrees'.

OK you probably get my point by now! I'm just confused as to how this is implemented in practice.

Thanks

edit: Just to clarify, I am talking about an online way of achieving this. I realise that I could write some scripts for them that query from their desktop to the site, such as :
Code:
declare namespace geo = "http://www.dur.ac.uk/geo-engineering";
declare namespace gml = "http://www.opengis.net/gml";
for $x in doc("http://www.example.com/SlopeCase/inventory/[ID]/Slope.xml")/geo:slopeData/geo:case/geo:slope/geo:toe/geo:location/gml:Point[@srsName='EPSG:27700']
return $x/gml:pos
I realise most people using the site will probably have XML editors that can perform queries such as this, but some may not and may wish to search current submissions that relate to their slope problem (case-based design help). I would therefore ultimately like to provide form-like XQueries - for example with the slope angle idea there could be a form which specifies the slope angle parameters they wish to search for, these submit in to the xQuery form and then show them the resultant list of matches cases. I guess all of this is possible and perhaps not that tricky ?

Last edited by jamesdurham; April 22nd, 2009 at 10:18 PM..
 
Old April 23rd, 2009, 04:35 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're overloading the thread - stick to one question per thread and there's a better chance they will all be answered.

>retrieve all of the Slope.xml files

If there are zillions of them, you need an XML database. If there are only 100 or so, then the collection() function can work quite well - in Saxon, for example, a collection URI can select files within a directory and you can do

collection('slopedir?select=*.xml')[f:slope(.) gt 30]
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 23rd, 2009, 06:22 AM
Authorized User
 
Join Date: Mar 2009
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default

I wasn't sure about creating a new thread for them because they are both tied in to this question and how I deal with things.

I'm stearing clear of creating a database because the whole idea of what I am doing is to eventually allow for this data to be held all over the world and be queriable(!) so to speak. Practically though I'm having problems though just querying on page let alone one site. As I stated, I know some XQuery that can perform actions from my software but I'm hoping there are things are could simply implement on the site to allow for the same things.

I shall post the two questions separately in other threads. Cheers





Similar Threads
Thread Thread Starter Forum Replies Last Post
using href in xslt rajesh_css XSLT 0 September 18th, 2008 02:44 AM
Saving XML thru href link using XSLT kaukabhishek XSLT 16 June 25th, 2008 07:16 PM
Want to pass value at href in anchor tag on xslt Abhinavnaresh XSLT 4 February 21st, 2008 04:32 AM
href in xslt rahulsk1947 XSLT 2 May 18th, 2007 07:39 PM
XML value in HRef link aware XSLT 3 January 8th, 2007 08:52 AM





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