 |
| 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
|
|
|
|

May 2nd, 2007, 09:00 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
XPath & XSLT problem
Hello there,
my name is Andy and I a newbie-XSLT-starter along with a problem here. I presume that is pretty simple one, but stuck with it for a couple of days...
How do I query a namespaced element like this (bolded it out)?
Code:
<identity>
<dataIdentity>
<title xsi:type="PT_FreeText_PropertyType">
<gco:CharacterString>Dataset No17, 2004</gco:CharacterString>
<PT_FreeText>
<textGroup >
<LocalisedCharacterString locale="#xpointer(//*[@id='hu'])">Hungary language, 2004</LocalisedCharacterString>
</textGroup>
<textGroup>
<LocalisedCharacterString locale="#xpointer(//*[@id='fi'])">Finland language, 2004</LocalisedCharacterString>
</textGroup>
</PT_FreeText>
</title>
</dataIdentity>
</identity>
I try to query it the following way:
Code:
<xsl:template match="/">
<html>
<body>
<h3>Document</h3>
<table border="1">
<tr bgcolor="#ff9900">
<th align="left">Data</th>
</tr>
<tr>
<td> <xsl:value-of select="//title/gco:CharacterString"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
And got no luck. Everything is fine as long as the element is wrapped without any namespaces, like this (assuming title wasn't empty):
Code:
"//identity/dataIdentity/title/text()"
Any input or guidelines would be appreciated.
Thank your for reading.
Andy
|
|

May 2nd, 2007, 09:38 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Read the first post in this forum to get the link about namespaces. As you're beginning now is the time to get out of the habit of starting paths with //, it's an expensive search. Just use /identity/dataIdentity/title/text() but with the namespace prefix added.
--
Joe ( Microsoft MVP - XML)
|
|

May 2nd, 2007, 01:10 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
What you are doing is fine. Of course the stylesheet needs to contain a declaration of the gco namespace, that is something like
xmlns:gco="....."
where the namespace URI matches what's declared in the source document.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 3rd, 2007, 12:51 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Joe,
I've read couple of links about namespaces. And I do know the expensiveness of using //. I prefer doing the task first, then optimizing it.
Michael,
the namespace is declared as follows:
Code:
<xsl:stylesheet version="1.0"
other declarations...
xmlns:gco="http://www.isotc211.org/2005/gco">
Any other ideas?
Just got O'Reilly's XSLT book, will try to deal with the problem.
Thanks.
Andy
|
|

May 3rd, 2007, 01:18 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
At this point the best strategy is to post a complete but minimal source document and stylesheet that illustrates the problem, it should then be easy to see what you are doing wrong.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

May 3rd, 2007, 01:41 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This is the XML document, based on ISO11519. Didn't feel like posting the whole document is necessary. And... don't worry -- the doc is well-formed.
OK, here we go. The XML file:
Code:
<?xml version="1.0" encoding="ISO-8859-2"?>
<?xml-stylesheet type="text/xsl" href="md_gos.xsl"?>
<MD_Metadata
xmlns="http://www.isotc211.org/2005/gmd"
xmlns:gco="http://www.isotc211.org/2005/gco"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.isotc211.org/2005/gmd C:/ISO19139schemas/gmd/gmd.xsd
http://www.isotc211.org/2005/gco C:/ISO19139schemas/gco/gco.xsd
http://www.opengis.net/gml C:/ISO19139schemas/gml/gml.xsd
http://www.w3.org/1999/xlink C:/ISO19139schemas/xlink/xlinks.xsd">
<fileIdentifier>
<gco:CharacterString>MD_gos00021</gco:CharacterString>
</fileIdentifier>
<language>
<gco:CharacterString>en</gco:CharacterString>
</language>
<characterSet>
<MD_CharacterSetCode codeListValue="8859part2" codeList="./resources/codeList.xml#MD_CharacterSetCode">8859part2</MD_CharacterSetCode>
</characterSet>
<hierarchyLevel>
<MD_ScopeCode codeList="./resources/codeList.xml#MD_ScopeCode" codeListValue="dataset">dataset</MD_ScopeCode>
</hierarchyLevel>
<contact xlink:href="CI_RP-sores.xml">
<CI_ResponsibleParty>
<individualName>
<gco:CharacterString>Sõrés L#303;szló</gco:CharacterString>
</individualName>
<organisationName>
<gco:CharacterString>Eotvos Lorand Geophysical Institute (ELGI)</gco:CharacterString>
</organisationName>
<positionName>
<gco:CharacterString>
geophysicist</gco:CharacterString>
</positionName>
<contactInfo>
<CI_Contact>
<address xlink:href="CI_ADD-sores.xml"/>
</CI_Contact>
</contactInfo>
<role>
<CI_RoleCode codeList="./resources/codeList.xml#CI_RoleCode" codeListValue="custodian"/>
</role>
</CI_ResponsibleParty>
</contact>
<dateStamp>
<gco:Date>2007-01-08</gco:Date>
</dateStamp>
<metadataStandardName>
<gco:CharacterString>ISO 19115</gco:CharacterString>
</metadataStandardName>
<metadataStandardVersion>
<gco:CharacterString>2003</gco:CharacterString>
</metadataStandardVersion>
<dataSetURI>
<gco:CharacterString>http://www.elgi.hu/geomind/getGos?gos-00021</gco:CharacterString>
</dataSetURI>
<referenceSystemInfo>
<MD_ReferenceSystem>
<referenceSystemIdentifier>
<RS_Identifier>
<code>
<gco:CharacterString>EPSG:23700</gco:CharacterString>
</code>
</RS_Identifier>
</referenceSystemIdentifier>
</MD_ReferenceSystem>
</referenceSystemInfo>
<identificationInfo>
<MD_DataIdentification>
<citation>
<CI_Citation>
<title xsi:type="PT_FreeText_PropertyType">
<gco:CharacterString>Dataset of the TDEM measurement campaign in Uveghuta, 2002</gco:CharacterString>
<PT_FreeText>
<textGroup >
<LocalisedCharacterString locale="#xpointer(//*[@id='hu'])">A 2002-ben Ãveghut#303;n végzett tranziens mérési kamp#303;ny adatai</LocalisedCharacterString>
</textGroup>
<textGroup>
<LocalisedCharacterString locale="#xpointer(//*[@id='fi'])">Kelle hulla tejbe pappi, Glassahutta 2002</LocalisedCharacterString>
</textGroup>
</PT_FreeText>
</title>
The XSL file:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.isotc211.org/2005/gmd"
xmlns:gco="http://www.isotc211.org/2005/gco"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="/">
<html>
<body>
<h3>Meta document (ISO-19115)</h3>
<table border="1">
<tr bgcolor="#ff9900">
<th align="left">Title</th>
<th align="left">Alternate title</th>
<th align="left">West</th>
<th align="left">East</th>
<th align="left">South</th>
<th align="left">North</th>
</tr>
<tr>
<td> <xsl:value-of select="/MD_Metadata/identificationInfo/MD_DataIdentification/citation/CI_Citation/title/gco:CharacterString"/></td>
<td> ... </td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Finally, I make the transformation using batch file (run.bat):
Code:
java -cp "./lib/serializer.jar;./lib/xalan.jar;./lib/xercesImpl.jar;./lib/xml-apis.jar" org.apache.xalan.xslt.Process -IN %1 -OUT %2
To check it, I run the following string in a command line: "run.bat theXML.xml theXMLout.html"
The output, displayed in Firefox 2.0.0.3 browser (same with IE 6.0):
http://www.geocities.com/quakerlt/meta.JPG
Thanks for helping.
P.s. Doug Tidwell just mentioned you, Michael, -- as an author of Saxon -- in his XSLT book. That's nice to hear :)
|
|

May 3rd, 2007, 02:17 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Your document has a default namespace declared, 'xmlns="http://www.isotc211.org/2005/gmd"', so you need so invent a prefix, such as 'default' and declare it in the stylesheet: 'xmlns:default="http://www.isotc211.org/2005/gmd"'.
Then any elements without a prefix in the XML need to be selected via dummy:<element name>.
For more examples take a look at the links I referred to earlier regarding default namespace.
--
Joe ( Microsoft MVP - XML)
|
|

May 3rd, 2007, 02:50 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Joe,
thanks for the problem solving hint. That made my day.
Again, thanks for the help guys!
Cheers,
Andy
|
|
 |