XSL namespaces and xsl:for-each - xslt

I'm currently stuck trying to use XSL to transform a XML document into HTML. The XML document uses namespaces and I don't really have too much experience with XSL let alone namespaces. Basically all I want to do is grab every instance of s:treatment and output this as a list. I've changed data so not to expose the website I'm doing this for. I'm using Classic ASP (can't update to ASP.NET) to transform the XML on the server, so the XSL has to be version 1 :(
Any help would be really appreciated here as I just can't figure out what's going wrong.
Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">a</title>
<id>a</id>
<rights type="text">© Crown Copyright 2009</rights>
<updated>2011-01-19T11:23:25Z</updated>
<category term="Service"/>
<author>
<name>c</name>
<uri>http://www.meh.com</uri>
<email>erwt</email>
</author>
<complete xmlns="http://purl.org/syndication/history/1.0"/>
<entry>
<id>http://meh.com/services</id>
<title type="text">title</title>
<updated>2010-06-18T19:52:12+01:00</updated>
<link rel="self" title="title" href="meh"/>
<link rel="alternate" title="title" href="id"/>
<content type="application/xml">
<s:service xmlns:s="http://syndication.nhschoices.nhs.uk/services">
<s:type code="S">h</s:type>
<s:deliverer>j</s:deliverer>
<s:parent>k</s:parent>
<s:treatments>
<s:treatment>fissure</s:treatment>
<s:treatment>fistula</s:treatment>
<s:treatment>liver</s:treatment>
<s:treatment>pancreas</s:treatment>
<s:treatment>Cirrhosis</s:treatment>
<s:treatment>Coeliac disease</s:treatment>
<s:treatment>Crohn's disease</s:treatment>
<s:treatment>Diagnostic endoscopy of the stomach</s:treatment>
<s:treatment>Diverticular problems</s:treatment>
<s:treatment>Gastrectomy</s:treatment>
<s:treatment>Gastroenteritis</s:treatment>
<s:treatment>Gastroenterology</s:treatment>
<s:treatment>Gastroesophageal reflux disease(GORD)</s:treatment>
<s:treatment>Hepatitis</s:treatment>
<s:treatment>Hepatitis A</s:treatment>
<s:treatment>Hepatitis B</s:treatment>
<s:treatment>Hepatitis C</s:treatment>
<s:treatment>Hernia hiatus</s:treatment>
<s:treatment>Ileostomy</s:treatment>
<s:treatment>Irritable bowel syndrome</s:treatment>
<s:treatment>Liver disease (alcoholic)</s:treatment>
<s:treatment>Obesity</s:treatment>
<s:treatment>Pancreatitis</s:treatment>
<s:treatment>Peptic ulcer</s:treatment>
<s:treatment>Peritonitis</s:treatment>
<s:treatment>Primary biliary cirrhosis</s:treatment>
<s:treatment>Surgery for haemorrhoids</s:treatment>
<s:treatment>Therapeutic endoscopy on the stomach</s:treatment>
<s:treatment>Ulcerative colitis</s:treatment>
</s:treatments>
<s:phone>020 8</s:phone>
<s:fax>020 8</s:fax>
<s:email>jj</s:email>
<s:website>oiyi</s:website>
</s:service>
</content>
</entry>
</feed>
As you can see it uses the Atom namespace, Purl syndication namespace and NHS Choices namespace only the NHS Choices namespace actually uses the prefix though, this is what is confusing me really. How would I declare the other namespaces and do I even need to?
Here's my XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://syndication.nhschoices.nhs.uk/services">
<ul>
<xsl:template match="/">
<xsl:for-each select="feed/entry/content/s:service/s:treatments/s:treatment">
<li><xsl:text></xsl:text></li>
</xsl:for-each>
</xsl:template>
</ul>
</xsl:stylesheet>
This XSL was taken from the w3schools example so apologies if it's bare.
Any ideas what I need to do to make this work?
Thanks,
Colin

First of all your XSLT is not valid at all (uloutside xsl:template is not valid XSL).
Further more as there is a default namespace on your feed tag you have also to define this in your xslt. You also better make usage of mathcing templates intead of for-each loops in CSLT.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://syndication.nhschoices.nhs.uk/services"
xmlns:a="http://www.w3.org/2005/Atom" exclude-result-prefixes="a s">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<ul>
<xsl:apply-templates
select="a:feed/a:entry/a:content/s:service/s:treatments/s:treatment" />
</ul>
</xsl:template>
<xsl:template match="s:treatment">
<li>
<xsl:value-of select="." />
</li>
</xsl:template>
</xsl:stylesheet>

Related

xslt not showing results. xpath or ns wrong?

i have the following xml and xslt to render it, but got no results. I checked again and again and see not path problem, and the xsl went through the compiler. so I am not sure if it's namespace problem or something else. many thx!
XML file
<?xml version="1.0" encoding="UTF-8"?>
<bibdataset xsi:schemaLocation="http://www.elsevier.com/xml/ani/ani http://www.elsevier.com/xml/ani/embase_com.xsd"
xmlns="http://www.elsevier.com/xml/ani/ani"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ce="http://www.elsevier.com/xml/ani/common"
xmlns:ait="http://www.elsevier.com/xml/ani/ait">
<item>
<bibrecord>
<item-info>
<itemidlist><ce:doi>10.1258/0268355042555000</ce:doi>
</itemidlist>
</item-info>
<head>
<citation-title>
<titletext xml:lang="en" original="y">Effect of seasonal variations on the emergence of deep venous thrombosis of the lower extremity
</titletext>
</citation-title>
<abstracts>
<abstract xml:lang="en" original="y">
<ce:para>Objective: We aimed to determine the role of seasonal and meteorological variations in the incidence of lower extremity
</ce:para>
</abstract>
</abstracts>
</head>
</bibrecord>
</item>
</bibdataset>
XSLT file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ce="http://www.elsevier.com/xml/ani/common"
xmlns:ait="http://www.elsevier.com/xml/ani/ait">
<xsl:output indent="yes" omit-xml-declaration="no"
media-type="application/xml" encoding="UTF-8" />
<xsl:template match="/">
<searchresult>
<xsl:apply-templates
select="/bibdataset/item/bibrecord" />
</searchresult>
</xsl:template>
<xsl:template match="bibrecord">
<document>
<title><xsl:value-of select="head/citation-title/titletext" /></title>
<snippet>
<xsl:value-of select="head/abstracts/abstract/ce:para" />
</snippet>
<url>
<xsl:variable name="doilink" select="item-info/itemidlist/ce:doi"/>
<xsl:value-of
select="concat('http://dx.doi.org/', $doilink)" />
</url>
</document>
</xsl:template>
</xsl:stylesheet>
This is indeed an issue with namespaces. In your XML, you have declared a default namespace meaning the root element, and all its descendants, and in this namespace.
<bibdataset .xmlns="http://www.elsevier.com/xml/ani/ani" ...
Now, in your XSLT, you have also declared this namespace, but without a prefix.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
This means it only applies to the elements you are outputting, so your searchresult element gets output in this namespace
<searchresult xmlns="http://www.elsevier.com/xml/ani/ani"
However, it doesn't apply to the xpath expression in your XSLT, and so these are looking for elements in your input XML with no namespace.
In XSLT 2.0 the solution would be to simply declare an "xpath-default-namespace"
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
xpath-default-namespace="http://www.elsevier.com/xml/ani/ani" ...
In XSLT 1.0, you will have to declare the namespace with a prefix, and use this prefix in all the xpath expressions.
Try this XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
xmlns:ani="http://www.elsevier.com/xml/ani/ani"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ce="http://www.elsevier.com/xml/ani/common"
xmlns:ait="http://www.elsevier.com/xml/ani/ait"
exclude-result-prefixes="ani">
<xsl:output indent="yes" omit-xml-declaration="no"
media-type="application/xml" encoding="UTF-8" />
<xsl:template match="/">
<searchresult>
<xsl:apply-templates
select="/ani:bibdataset/ani:item/ani:bibrecord" />
</searchresult>
</xsl:template>
<xsl:template match="ani:bibrecord">
<document>
<title><xsl:value-of select="ani:head/ani:citation-title/ani:titletext" /></title>
<snippet>
<xsl:value-of select="ani:head/ani:abstracts/ani:abstract/ce:para" />
</snippet>
<url>
<xsl:variable name="doilink" select="ani:item-info/ani:itemidlist/ce:doi"/>
<xsl:value-of
select="concat('http://dx.doi.org/', $doilink)" />
</url>
</document>
</xsl:template>
</xsl:stylesheet>
Note that you can remove the line xmlns="http://www.elsevier.com/xml/ani/ani" but that would mean your searchresult (and other) elements would be output with no namespace, so you would need to output it as <ani:searchresult> if you wanted it in the given namespace.

xsl not rendering results

I have to make an xsl for an xml output exported from a database. The name tags in xml have a prefix bib followed by a colon (as in bib:), and it was defined in xml. But I still got an xsl compiler error saying bib: is not declared. So I added the declaration in xsl. this time the error went away, but the result comes out zero, and I checked the path that is correct. I also tried to exclude "bib:" prefix in xsl after declaration but got the same zero result. I am new to xsl so I don't know what is wrong here. These are my files. Thanks very much.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<embasexmllist>
<cards items="1">
<bib:card items="0" xmlns:bib="http://elsevier.co.uk/namespaces/2001/bibliotek">-
<bib:cardfields>
<bib:Fulltext>
<bib:DOI>10.1371/journal.pone.0068303</bib:DOI>
</bib:Fulltext>
<bib:Title>Mesothelin Virus-Like Particle Immunization Controls Pancreatic Cancer Growth through CD8+ T Cell Induction and Reduction in the Frequency of CD4+foxp3+ICOS- Regulatory T Cells
</bib:Title>
</bib:cardfields>
</bib:card>
</cards>
</embasexmllist>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bib="http://www.bib.com/xml">
<xsl:output indent="yes" omit-xml-declaration="no"
media-type="application/xml" encoding="UTF-8" />
<xsl:template match="/">
<searchresult>
<xsl:apply-templates
select="/embasexmllist/cards/bib:card/bib:cardfields" />
</searchresult>
</xsl:template>
<xsl:template match="bib:cardfields">
<document>
<title><xsl:value-of select="bib:Title" /></title>
<snippet>
<xsl:value-of select="bib:Title" />
</snippet>
<url>
<xsl:variable name="doi" select="bib:Fulltext/bib:DOI"/>
<xsl:value-of
select="concat('http://dx.doi.org/', $doi)" />
</url>
</document>
</xsl:template>
</xsl:stylesheet>
The prefix defines a namespace for the XML elements.
For your stylesheet to work, the namespace declaration needs to match that in your input XML. Replace
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bib="http://www.bib.com/xml">
with
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bib="http://elsevier.co.uk/namespaces/2001/bibliotek">
This results in the following output XML:
<?xml version="1.0" encoding="utf-8"?>
<searchresult xmlns:bib="http://elsevier.co.uk/namespaces/2001/bibliotek">
<document>
<title>
Mesothelin Virus-Like Particle Immunization Controls Pancreatic Cancer Growth through CD8+ T Cell Induction and Reduction in the Frequency of CD4+foxp3+ICOS- Regulatory T Cells
</title>
<snippet>
Mesothelin Virus-Like Particle Immunization Controls Pancreatic Cancer Growth through CD8+ T Cell Induction and Reduction in the Frequency of CD4+foxp3+ICOS- Regulatory T Cells
</snippet>
<url>http://dx.doi.org/10.1371/journal.pone.0068303</url>
</document>
</searchresult>

Find and replace a string inside an image path

I have a rss xml file that looks like below with the tag looping many times. I would like to replace the letter 's' with 'm' in the url inside the tag, So http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_s.jpg becomes http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_m.jpg
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:creativeCommons="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html" xmlns:flickr="urn:flickr:user" version="2.0">
<channel>
<title>CCGalleria Pool</title>
<link>http://www.flickr.com/groups/ccgalleria/pool/</link>
<item>
<title>Hampton Court Palace Gardens</title>
<link>http://www.flickr.com/photos/dksesh/9593294742/in/pool-1540822#N20</link>
<description>
<p>
dksesh
has added a photo to the pool:</p>
<p>
<a href="http://www.flickr.com/photos/dksesh/9593294742/" title="Hampton Court Palace Gardens">
<img src="http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_m.jpg" width="240" height="107" alt="Hampton Court Palace Gardens"/>
</a>
</p>
</description>
<pubDate>Sun, 25 Aug 2013 10:33:09 -0700</pubDate>
<dc:date.Taken>2013-08-10T16:49:05-08:00</dc:date.Taken>
<author flickr:profile="http://www.flickr.com/people/dksesh/">nobody#flickr.com (dksesh)</author>
<guid isPermaLink="false">tag:flickr.com,2004:/grouppool/1540822#N20/photo/9593294742</guid>
<media:content url="http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_b.jpg" type="image/jpeg" height="456" width="1024"/>
<media:title>Hampton Court Palace Gardens</media:title>
<media:thumbnail url="http://farm4.staticflickr.com/3802/9593294742_a38fca47c7_s.jpg" HEIGHT="75" WIDTH="75"/>
<media:credit ROLE="photographer">dksesh</media:credit>
<creativeCommons:license>http://creativecommons.org/licenses/by-nd/2.0/deed.en_GB</creativeCommons:license>
</item>
</channel>
</rss>
I have a code something like below, but didn't work. Can someone help? Thanks very much. Newly changed code below.
<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:media="http://search.yahoo.com/mrss/">
<xsl:output method="html" />
<xsl:template match="/">
<xsl:for-each select="rss/channel/item">
<xsl:variable name="newurl" select="replace(media:thumbnail/#url,'_s.jpg','_m.jpg')"/>
<img src="{$newurl}" style="margin:5px 5px" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Your stylesheet sample showns the version as "2.0" which is good, because there is a replace function you can use in XSLT 2.0 that will probably do the trick here
<xsl:variable name="newurl" select="replace(media:thumbnail/#url,'_s.jpg','_m.jpg')"/>
<img src="{$newurl}" style="margin:5px 5px" />
Note that the second argument of the replace function can actually be a regular expression, if you wanted more control over what 's' needed to be replaced.
As an aside, do note the correct use of Attribute Value Templates here, signified by the curly braces { }. You use them in outputting the src attribute of the img element, but not within the translate/replace function.
Here is the full XSLT in this case:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:media="http://search.yahoo.com/mrss/">
<xsl:output method="html" />
<xsl:template match="/">
<xsl:for-each select="rss/channel/item">
<xsl:variable name="newurl" select="replace(media:thumbnail/#url,'_s.jpg','_m.jpg')"/>
<img src="{$newurl}" style="margin:5px 5px" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Embedding static CDATA with its tag in XSLT

I need to output from the XSL a static CDATA construct embedded in the XSL, not from the XML that I am transforming. For example...
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<!-- ================================================== -->
<xsl:template match="/">
<Document>
<text><![CDATA[
<b>static</b>
<br/><br/>
text
<br/><br/>
]]>
</text>
<xsl:apply-templates select="//tag"/>
</Document>
</xsl:template>
<!-- ================================================== -->
<xsl:template match="tag">
So on and so forth...
</xsl:template>
<!-- ================================================== -->
</xsl:stylesheet>
I want this to output...
<?xml version="1.0"?>
<Document>
<text><![CDATA[
<b>static</b>
<br/><br/>
text
<br/><br/>
]]>
</text>
So on and so forth...
</Document>
But what I get is...
<?xml version="1.0"?>
<Document>
<text>
<b>static</b>
<br/><br/>
text
<br/><br/>
</text>
So on and so forth...
</Document>
I've tried several combinations of escaping the text and entities, but none seem to work.
Use
<xsl:output cdata-section-elements="text" />
to enforce CDATA for certain elements (spec).
In any case, what you currently get is equivalent to a CDATA section and it should not bother you. (i.e.: If it's bothering you for optical reasons, then get over it. If it is bothering you for technical reasons, fix them.)

xsl:include template with no default namespace causes xmlns=""

I've got a problem with xsl:include and default namespaces which is causing the final xml document contain nodes with the xmlns=""
In this synario I have 1 source document which is Plain Old XML and doesn't have a namespace:
<?xml version="1.0" encoding="UTF-8"?>
<SourceDoc>
<Description>Hello I'm the source description</Description>
<Description>Hello I'm the source description 2</Description>
<Description/>
<Title>Hello I'm the title</Title>
</SourceDoc>
This document is transformed into 2 different xml documents each with their own default namespace.
First Document:
<?xml version="1.0" encoding="utf-8"?>
<OutputDocType1 xmlns="http://MadeupNS1">
<Description >Hello I'm the source description</Description>
<Description>Hello I'm the source description 2</Description>
<Title>Hello I'm the title</Title>
</OutputDocType1>
Second Document:
<?xml version="1.0" encoding="utf-8"?>
<OutputDocType2 xmlns="http://MadeupNS2">
<Description>Hello I'm the source description</Description>
<Description>Hello I'm the source description 2</Description>
<DocTitle>Hello I'm the title</DocTitle>
</OutputDocType2>
I want to be able to re-use the template for descriptions in both of the transforms. As it's the same logic for both types of document. To do this I created a template file which was xsl:included in the other 2 transformations:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="Description[. != '']">
<Description>
<xsl:value-of select="."/>
</Description>
</xsl:template>
</xsl:stylesheet>
Now the problem here is that this shared transformation can't have a default Namespace as it will be different depending on which of the calling transformations calls it.
E.g. for First Document Transformation:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="SourceDoc">
<OutputDocType1 xmlns="http://MadeupNS1">
<xsl:apply-templates select="Description"/>
<xsl:if test="Title">
<Title>
<xsl:value-of select="Title"/>
</Title>
</xsl:if>
</OutputDocType1>
</xsl:template>
<xsl:include href="Template.xsl"/>
</xsl:stylesheet>
This actually outputs it as follows:
<?xml version="1.0" encoding="utf-8"?>
<OutputDocType1 xmlns="http://MadeupNS1">
<Description xmlns="">Hello I'm the source description</Description>
<Description xmlns="">Hello I'm the source description 2</Description>
<Title>Hello I'm the title</Title>
</OutputDocType1>
Here is the problem. On the description Lines I get an xmlns=""
Does anyone know how to solve this issue?
Thanks
Dave
Your first xslt, which contains the literal result element Description has no default namespace. This element is therefore in no namespace, and is being explicitly rendered as such via xmlns="".
Section 6.2 of Namespaces in XML 1.0 says that:
The attribute value in a default
namespace declaration MAY be empty.
This has the same effect, within the
scope of the declaration, of there
being no default namespace.
In order to control the namespace generated in the included stylesheet you will need to pass the namespace-uri through to its templates, using a variable or param.
<!-- in the included stylesheet -->
<xsl:template match="Description[. != '']">
<xsl:element name="Description" namespace="{$output-namespace}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<!--
and add this to your First Document Transformation stylesheet
as a top level element under xsl:stylesheet
-->
<xsl:variable name="output-namespace" select="'http://MadeupNS1'"/>