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.
Related
My XSLT is outputiung some tags with xmlns:x="http://something" attribute... How to avoid this redundant attribute? The output XML never use, neither in a the x:tag, nor in an x:attribute.
EXAMPLE OF XML:
<root><p>Hello</p><p>world</p></root>
EXAMPLE OF XSL:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output encoding="UTF-8" method="xml" version="1.0" indent="no"/>
<xsl:template match="root"><foo>
<xsl:for-each select="p">
<p><xsl:value-of select="." /></p>
</xsl:for-each></foo>
<xsl:for-each select="x">
<link xlink:href="{x}" />
</xsl:for-each></foo>
</xsl:template>
EXAMPLE OF XML OUTPUT:
<foo>
<p xmlns:xlink="http://www.w3.org/1999/xlink">Hello</p>
<p xmlns:xlink="http://www.w3.org/1999/xlink">world</p>
</foo>
The xmlns:xlink is a overhead, it is not used!
A typical case where XSLT must use namespace but the output not:
<xsl:value-of select="php:function('regFunction', . )" />
As Dimitre has already said, if you are not using the xlink namespace anywhere in your XSLT, you should just remove its namespace declaration. If, however, your XSLT is actually using it somewhere that you haven't shown us, you can prevent it from being output by using the exclude-result-prefixes attribute:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink"
exclude-result-prefixes="xlink">
Just remove this namespace declaration from the xsl:stylesheet instruction -- it isn't used (and thus necessary) at all:
xmlns:xlink="http://www.w3.org/1999/xlink"
The whole transformation now becomes:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="xml" version="1.0" indent="no"/>
<xsl:template match="root"><foo>
<xsl:for-each select="p">
<p class="a"><xsl:value-of select="." /></p>
</xsl:for-each></foo>
</xsl:template>
</xsl:transform>
and when applied on the provided XML document:
<root><p>Hello</p><p>world</p></root>
produces result that is free of namespaces:
<foo>
<p class="a">Hello</p>
<p class="a">world</p>
</foo>
I have the following xml:
<RootNode xmlns="http://someurl/path/path/path">
<Child1>
<GrandChild1>Value</GrandChild1>
<!-- Lots more elements in here-->
</Child1>
</RootNode>
I have the following xslt:
<xsl:stylesheet version="1.0" xmlns="http://someurl/path/path/path" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<xsl:for-each select="RootNode/Child1">
<NewNodeNameHere>
<xsl:value-of select="GrandChild1"/>
</NewNodeNameHere>
<!-- lots of value-of tags in here -->
</xsl:for-each>
</NewChild1>
</NewRootNode >
</xsl:template>
</xsl:stylesheet>
The problem: this is the my result:
<?xml version="1.0" encoding="utf-8"?>
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1 />
</NewRootNode>
I am expecting to see:
<?xml version="1.0" encoding="utf-8"?>
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<NewNodeNameHere>Value</NewNodeNameHere>
<!-- Other new elements with values from the xml file -->
</NewChild1>
</NewRootNode>
I am missing of the information inside of NewChild1 that should be there.
I think my for-each select is correct, so the only thing I can think of is that there is a problem with the namespace in the Xml and the namespace in the xslt. Can anybody see what I'm doing wrong?
The problem is caused by the namespaces.
Since the xml defines xmlns="http://someurl/path/path/path", it is not in the default namespace anymore.
You can define that namespace with an name like xmlns:ns="http://someurl/path/path/path" in the xsl, and then use that name in the XPath expression.
The following works for me:
<xsl:stylesheet version="1.0" xmlns:ns="http://someurl/path/path/path" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<xsl:for-each select="ns:RootNode/ns:Child1">
<NewNodeNameHere>
<xsl:value-of select="ns:GrandChild1"/>
</NewNodeNameHere>
<!-- lots of value-of tags in here -->
</xsl:for-each>
</NewChild1>
</NewRootNode >
</xsl:template>
</xsl:stylesheet>
The stylesheet namespace should be http://www.w3.org/1999/XSL/Transform instead of http://someurl/path/path/path.
Also, since the input XML uses a namespace all your XPath expressions should be namespace-qualified:
<xsl:template match="/" xmlns:ns1="http://someurl/path/path/path">
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<xsl:for-each select="ns1:RootNode/ns1:Child1">
<NewNodeNameHere>
<xsl:value-of select="ns1:GrandChild1"/>
</NewNodeNameHere>
<!-- lots of value-of tags in here -->
</xsl:for-each>
</NewChild1>
</NewRootNode>
</xsl:template>
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'"/>
Here is a snip-it of the XML:
<?xml version="1.0" encoding="iso-8859-1" ?>
<NetworkAppliance id="S123456">
<Group id="9">
<Probe id="1">
<Value>74.7</Value>
</Probe>
</NetworkAppliance>
I want to get the single point value of 74.7. There are many groups with unique ID's and many Probes under that group with unique ID's each with values.
I am looking for example XSLT code that can get me this one value. Here is what i have that does not work:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="3.2" />
<xsl:template match="NetworkAppliance">
<xsl:apply-templates select="Group[#id='9']"/>
</xsl:template>
<xsl:template match="Group">
Temp: <xsl:value-of select="Probe[#id='1']/Value"/>
<br/>
</xsl:template>
</xsl:stylesheet>
Here is what worked for me in the end:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="NetworkAppliance/Group[#id=9]/Probe[#id=1]">
Value: <xsl:value-of select="Value" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Don't forget that you can do select several levels at once. Fixing your XML to:
<?xml version="1.0" encoding="iso-8859-1" ?>
<NetworkAppliance id="S123456">
<Group id="9">
<Probe id="1">
<Value>74.7</Value>
</Probe>
</Group>
</NetworkAppliance>
and using this stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="3.2" />
<xsl:template match="/">
Temp: <xsl:value-of select="//Group[#id='9']/Probe[#id='1']/Value"/>
<br/>
</xsl:template>
</xsl:stylesheet>
we can pick out that one item you're interested in.
Points to note:
The // part of the expression means that the search for Group nodes takes place throughout the whole tree, finding Group nodes at whatever depth they're at.
The [#id='9'] part selects those Group nodes with id of 9
The Probe[#id='1'] part immediately after that selects those children of the Group nodes it found where the id is 1, and so on.
<xsl:value-of select="/NetworkAppliance/Group[#id=9]/Probe[#id=1]/Value"/>
XSLT is just one of the tools in the box, and nothing without XPath.
the xpath for value of a node is /node/text()
So
<xsl:value-of select="Probe[#id='1']/text()"/>
For unknown reason max function doesn't work.
XML input file:
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<numbers>
<number>3</number>
<number>5</number>
<number>10</number>
<number>1</number>
</numbers>
XSL input file
test.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" >
<xsl:output method="xml" indent="yes" />
<xsl:template match="/numbers">
<numbers>
<xsl:value-of select="/numbers/number" />
fn:max(2, 3)
</numbers>
</xsl:template>
</xsl:stylesheet>
Output.xml
<?xml version="1.0" encoding="UTF-8"?>
<numbers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/02/xpath-functions">3
fn:max(2, 3)
</numbers>
Input file is not important here, but I would like to have '3' instead of fn:max(2, 3). How to do it?
for this XSL file:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" >
<xsl:output method="xml" indent="yes" />
<xsl:template match="/numbers">
<numbers>
<xsl:value-of select="/numbers/number" />
fn:max(2, 3)
<xsl:value-of select="max(/numbers/number)"/>
</numbers>
</xsl:template>
</xsl:stylesheet>
the following error occurs:
SystemId Unknown; Line #13; Column #49; Could not find function: max
SystemId Unknown; Line #13; Column #49; function token not found.
(Location of error unknown)java.lang.NullPointerException
(Location of error unknown)XSLT Error (javax.xml.transform.TransformerException)
: No xml-stylesheet PI found in: test.xml
Exception in thread "main" java.lang.RuntimeException: No xml-stylesheet PI foun
d in: test.xml
at org.apache.xalan.xslt.Process.doExit(Process.java:1155)
at org.apache.xalan.xslt.Process.main(Process.java:1128)
I used Xalan - Version Xalan Java 2.7.1, Command: java org.apache.xalan.xslt.Process -in test.xml -xsl test.xsl -out output.xml
There are several problems: max() needs to be in a value-of, and that you've said xsl:stylesheet version="2.0" for Xalan, which only supports XSLT 1.0. For 2.0, you'd need Saxon 9.x.
Since max() isn't part of XSLT 1.0, you need to invoke the EXSLT extension support, which Xalan does have:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://exslt.org/math">
<xsl:template match="/numbers">
<xsl:value-of select="math:max(number)"/>
</xsl:template>
</xsl:stylesheet>
or
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://exslt.org/math">
<xsl:template match="/">
<xsl:value-of select="math:max(numbers/number)"/>
</xsl:template>
</xsl:stylesheet>
You've put fn:max(2,3) in a text block. Nothing is going to interpret that. You need to put functions in value-of expressions if you want them to be evaluated.
Lavino,
Thanks for the response. I don't know why but I was pretty sure that Xalan supports 2.0... I've tested it and it works for Saxon 9.
You can use
<xsl:value-of select="max(number)" />
to get the max of all numbers.
Soln:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/numbers">
<numbers>
<max>
<xsl:value-of select="max(number)"/>
</max>
<xsl:apply-templates/>
</numbers>
</xsl:template>
<xsl:template match="number">
<number>
<xsl:value-of select="."/>
</number>
</xsl:template>
</xsl:stylesheet>
You can omit the number template and <xsl:apply-templates/> if its not reqd. This will be the output with the above xslt:
<?xml version="1.0" encoding="UTF-8"?>
<numbers xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<max>10</max>
<number>3</number>
<number>5</number>
<number>10</number>
<number>1</number>
</numbers>