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>
Related
I'm new with XSL, but it's ok, but it's the first time I need to do something with namespace, and I'm totally out, can someone explain how to do this :
I have an XHTML like this :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="5C.xslt"?>
<!DOCTYPE rdf:RDF SYSTEM "http://purl.org/dc/schemas/dcmes-xml20000714.
dtd">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/">
<rdf:Description rdf:about="MyJPeg.jpg">
<dc:title>Find Info</dc:title>
<dc:contributor>Myself</dc:contributor>
<dcterms:created>2013-12-11</dcterms:created>
<dcterms:issued>2013-12-23</dcterms:issued>
</rdf:Description>
</rdf:RDF>
I need to validate if the issued date if = to 2013-10-10 (answer no)
My XSLT is :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:output method="html" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:template match="*">
<html><body><pre>
<xsl:value-of select="rdf/issued"/>
<xsl:if test="xxx = '2013-10-10' ">
</xsl:if>
</pre></body>
</html>
</xsl:template>
</xsl:stylesheet>
So I try to have ther value with this line :
<xsl:value-of select="rdf/issued"/>
(to see if I got it)
And to validate with this one :
<xsl:if test="xxx = '2013-10-10' ">
But I'm new with name space and I can't find out how to get my value,
Can someone help me ?
thanks
Question #2, the solution works, but :
If I want to validate if the date is HIGHER than instead of equal, how I can do that ? (I replace = by >), and I change my date to be higher and lower, and each time it doesn't work
<xsl:if test="rdf:Description/dcterms:issued > '2001-01-01' ">
Good job
</xsl:if>
What's wrong ?
thanks
In XML, an element with a namespace if different to an element with no namespace. For example, despite having the same "local" name of "RDF" the following two elements are different.
<RDF>Test</RDF>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">Test</RDF>
To access elements within a namespace in XSLT, you first have to declare the relevant namespaces in your XSTL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dcterms="http://purl.org/dc/terms/">
Then, where you have an xpath expression that refers to elements, you need to add in the prefix
<xsl:value-of select="rdf:Description/dcterms:issued"/>
(I took it as a typo in your question, but "issued" is a child of "Description" in your XML sample!).
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dcterms="http://purl.org/dc/terms/">
<xsl:output method="html" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:template match="rdf:RDF">
<html><body><pre>
<xsl:value-of select="rdf:Description/dcterms:issued"/>
<xsl:if test="rdf:Description/dcterms:issued = '2013-10-10' ">
</xsl:if>
</pre></body>
</html>
</xsl:template>
</xsl:stylesheet>
It is worth mentioning that the namespace prefix ("rdf:" in this case), does not have to be the same in the XML as it is in the XSLT. It is the namespace URI ("http://www.w3.org/1999/02/22-rdf-syntax-ns#") that has to match.
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.
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>
Parsing the following with the php xlst processor result in this error:
`Warning: DOMDocument::load(): Namespace prefix g on id is not defined in /var/www /mistermatic/spider/resources/xslt/car/google.xsl, line: 32 in /var/www/mistermatic/ads/PublicXML/makeXML.php on line 91`
How do I correctly define the namespace inside the rss node so that I can apply it to all the children I'm gonna manipulate?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
<xsl:template match="/">
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>Mistermatic</title>
<link>http://www.mistermatic.it</link>
<description>Multipubblicazione di annunci di automobili nuove ed usate</description>
<xsl:for-each select="//ad">
<item xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<xsl:apply-templates select="itemId"/>
<g:availability>in stock</g:availability>
<g:google_product_category><![CDATA[Veicoli a motore > Veicoli > Auto, autocarri e furgoni]]></g:google_product_category>
</item>
</xsl:for-each>
</channel>
</rss>
</xsl:template>
<xsl:template match="itemId">
<g:id>
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:value-of select="#value"/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</g:id>
</xsl:template>
</xsl:stylesheet>
Move the xmlns:g declaration up to the xsl:stylesheet element, making it visible throughout the stylesheet.
The problem is you are defining the namespace within your rss element, however, it is used elsewhere in your XSLT file. Try moving your namespace definition to the root of your XSLT document.
I have this simplified xml:
<?xml version="1.0" encoding="UTF-8"?>
<a>
<b>
<c>
<d>1</d>
<e>2</e>
</c>
</b>
<f>
<g>3</g>
</f>
</a>
This is the xslt i try to apply:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="a">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="b">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="c">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="d">
</xsl:template>
</xsl:stylesheet>
When I apply this sheet, I get output 2 3, which are the remaining textnodes. I've read about the built-in templates which get applied if it can't find a matching template, but in this case, it should find a template?
What is going on?
Edit:
In this case, i would expect to see nothing, because the templates are empty. But i get 2 3 in stead.
When you do <xsl:template match="d">, you tell the processor to ignore all nodes under <d>.
All other nodes are processed with default rules, including the text() one, which is to print the text.
That's why you see 23, and not 1.
Start from the root:
<xsl:template match="/a">
And specify either a mode (so that the default template does not get called, because it does not find a template for e, f and g) or define your own * template which does nothing at the end of the stylesheet:
<xsl:template match="*"/>