Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT Newb Question.... 1

Status
Not open for further replies.

goBoating

Programmer
Feb 8, 2000
1,606
US
Sorry if this has been covered. I've programed with Perl for years, a simple XSLT is kicking my butt. I need to spend more time studying but need to get this trick working quickly. So, please pardon my ignorance.

I have an XML document available here:
And, I'd like to pull the various pieces/parts of the XML out into a CSV that will get sucked into Excel. I need to compare the content of two different instances of the getCapabilities XML.

My newb knowledge of writing XSLTs is proving inadequate. I've written a few non-trivial XSLTs before with some effort. The top of the XML looks like:
Code:
<?xml version="1.0"?>
<Capabilities xmlns:gml="[URL unfurl="true"]http://www.opengis.net/gml"[/URL] xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] xmlns:swe="[URL unfurl="true"]http://www.opengis.net/swe/1.0.1"[/URL] xmlns:om="[URL unfurl="true"]http://www.opengis.net/om/1.0"[/URL] xmlns="[URL unfurl="true"]http://www.opengis.net/sos/1.0"[/URL] xmlns:sos="[URL unfurl="true"]http://www.opengis.net/sos/1.0"[/URL] xmlns:ows="[URL unfurl="true"]http://www.opengis.net/ows/1.1"[/URL] xmlns:ogc="[URL unfurl="true"]http://www.opengis.net/ogc"[/URL] xmlns:tml="[URL unfurl="true"]http://www.opengis.net/tml"[/URL] xmlns:sml="[URL unfurl="true"]http://www.opengis.net/sensorML/1.0.1"[/URL] xmlns:myorg="[URL unfurl="true"]http://www.myorg.org/features"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xsi:schemaLocation="[URL unfurl="true"]http://www.opengis.net/sos/1.0[/URL] [URL unfurl="true"]http://schemas.opengis.net/sos/1.0.0/sosAll.xsd"[/URL] version="1.0.0">
<!-- This is a PROTOTYPE service.  The information in this response is NOT complete. -->
  <ows:ServiceIdentification>
    <ows:Title>National Data Buoy Center SOS</ows:Title>
    <ows:Abstract>National Data Buoy Center SOS</ows:Abstract>
    <ows:Keywords>
      <ows:Keyword>Weather</ows:Keyword>
      <ows:Keyword>Ocean Currents</ows:Keyword>

      <ows:Keyword>Water Temperature</ows:Keyword>
      <ows:Keyword>Salinity</ows:Keyword>
      <ows:Keyword>Water Level</ows:Keyword>
      <ows:Keyword>Waves</ows:Keyword>
      <ows:Keyword>Winds</ows:Keyword>
    </ows:Keywords>

    <ows:ServiceType codeSpace="[URL unfurl="true"]http://opengeospatial.net">OGC:SOS</ows:ServiceType>[/URL]
    <ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
    <ows:Fees>NONE</ows:Fees>
    <ows:AccessConstraints>NONE</ows:AccessConstraints>
  </ows:ServiceIdentification>
  <ows:ServiceProvider>
    <ows:ProviderName>National Data Buoy Center</ows:ProviderName>

    <ows:ProviderSite xlink:href="[URL unfurl="true"]http://sdf.ndbc.noaa.gov/"/>[/URL]

I'm starting very modestly and trying to get the 'Title' like:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] xmlns:om="[URL unfurl="true"]http://www.opengis.net/om/1.0"[/URL] xmlns:gml="[URL unfurl="true"]http://www.opengis.net/gml/3.2"[/URL] xmlns:ioos="[URL unfurl="true"]http://www.noaa.gov/ioos/0.6.1"[/URL] xmlns:swe="[URL unfurl="true"]http://www.opengis.net/swe/1.0.2"[/URL] xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/Capabilities">
		<xsl:value-of select="ows:ServiceIdentification/ows:Title"/>
	</xsl:template>
</xsl:stylesheet>

But, instead of just getting the contents of the 'ows:Title' element, I get the entire match for the template.

I would like to get just the 'ows:Title'. If I can learn what I'm doing wrong there, I think it will break the log jam.

TIA

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
When I try:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] xmlns:om="[URL unfurl="true"]http://www.opengis.net/om/1.0"[/URL] xmlns:gml="[URL unfurl="true"]http://www.opengis.net/gml/3.2"[/URL] xmlns:ioos="[URL unfurl="true"]http://www.noaa.gov/ioos/0.6.1"[/URL] xmlns:swe="[URL unfurl="true"]http://www.opengis.net/swe/1.0.2"[/URL] xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<xsl:value-of select="Capabilities/ows:ServiceIdentification/ows:Title"/>
	</xsl:template>
</xsl:stylesheet>

XMLSpy tells me "This file is not valid. Error in XPATH expression. Invalid prefix." This refers to a first xsl:value-of line in the xslt.

If I remove the ows prefixes from the elements in that XPATH, XMLSpy runs the transform but obviously does not show a Title, because there is no 'Title', only an 'ows:Title' in the XML doc.



'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
>If I can learn what I'm doing wrong there, I think it will break the log jam.
I certainly hope so. The xsl you came up with is completely off your control and it is leaving everything to the mercy of default template match text.

This is the reductionist/minimal version showing all the "essential" and only "essential" elements for the purpose.
[tt]
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]"
xmlns:sos="[ignore][/ignore]"
xmlns:eek:ws="[ignore][/ignore]"
>
<xsl:eek:utput method="text" encoding="UTF-8" indent="yes"/>
<xsl:template match="/ows:Capabilities">
<xsl:value-of select="ows:ServiceIdentification/ows:Title"/>
</xsl:template>
</xsl:stylesheet>[/tt]
 
tsuji,
Thanks for the response. I tried several variations of 'templates' and ended up with the same problem.

I run into the same issue with your xsl code. It matches the 'ows:Capabilities' element and outputs the entire contents of the XML doc. I don't understand why.

Even if I comment out the xsl:value-of element in the XSLT like this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
    xmlns:sos="[URL unfurl="true"]http://www.opengis.net/sos/1.0"[/URL]
    xmlns:ows="[URL unfurl="true"]http://www.opengis.net/ows/1.1">[/URL]
<xsl:output method="text" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/ows:Capabilities">
        <!-- xsl:value-of select="ows:ServiceIdentification/ows:Title"/ -->
    </xsl:template>
</xsl:stylesheet>

I get the entire contents of the XML doc in my output. The reason I tried to work against the default template is because when I do, it does not output the entire XML doc.

I'm using XMLSpy to run the XSLT against the XML doc. I have several other XML docs following various schematta with associated XSLTs that work fine. Weird.



'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Sorry, goBoating. That is my fault. There is a typo or a case where my hands failed to follow my brain. I added sos namespace in the xsl:stylesheet element for the purpose, but ended up putting ows for Capabilities. It should be sos.Capabilities.
[tt]
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]"
xmlns:sos="[ignore][/ignore]"
xmlns:eek:ws="[ignore][/ignore]"
>
<xsl:eek:utput method="text" encoding="UTF-8" indent="yes"/>
<xsl:template match="/[red]sos[/red]:Capabilities">
<xsl:value-of select="ows:ServiceIdentification/ows:Title"/>
</xsl:template>
</xsl:stylesheet>[/tt]
 
Beautiful.
Thank you very much.



'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top