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.
Related
I have the following XML with a cdata tag that I would like to extract the text from?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cus:TestData xmlns:cus="http://test.namespace.com/data">
<![CDATA[testValue]]></cus:TestData >
How can I achieve this in XSLT?
I was briefly trying with the following
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/Name"/>"
</xsl:template>
</xsl:stylesheet>
But it doesn't seem to be working
Also the XML doesn't also have the same prefix or namespace, it changes
This is not really an issue with CData. Your XSLT is currently looking for an element called Name, under the root element, which does not exist in your XML. If your XML source is the one you are actually using, you can just do this...
<xsl:value-of select="/*"/>
But supposing your XML looked like this...
<cus:TestData xmlns:cus="http://test.namespace.com/data">
<cus:Name><![CDATA[testValue]]></cus:Name>
</cus:TestData>
Then, you would need to account for the namespace in your XSLT, as Name is in a namespace in your XML, but your XSLT is currently looking for a Name element in no namespace.
Something like this would do:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://test.namespace.com/data">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/c:Name"/>"
</xsl:template>
</xsl:stylesheet>
Note, the prefixes don't need to match, but the namespace URI does.
If the namespace URI could actually vary, you could do something like this instead...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
"<xsl:value-of select="/*/*[local-name() = 'Name']"/>"
</xsl:template>
</xsl:stylesheet>
This question looks like a duplicate of XPath query for GPX files with namespaces?, but I must be missing something because I can't seem to get a fairly simple style sheet to work. I have this input:
<?xml version="1.0" encoding="utf-8"?>
<gpx xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" creator="Groundspeak Pocket Query" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
<name>Ottawa Pocket Query</name>
<wpt lat="45.348517" lon="-75.825933">
<name>GC3HXAZ</name>
<desc>Craft maker box by FishDetective, Traditional Cache (2/2.5)</desc>
<url>http://www.geocaching.com/seek/cache_details.aspx?guid=e86ce3f5-9e75-48a6-b47e-9415101fc658</url>
<groundspeak:cache id="2893138" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
<groundspeak:name>Craft maker box</groundspeak:name>
<groundspeak:difficulty>2</groundspeak:difficulty>
<groundspeak:terrain>2.5</groundspeak:terrain>
</groundspeak:cache>
</wpt>
</gpx>
And a stylesheet that looks like this:
<?xml version="1.0"?>
<!-- -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:groundspeak="http://www.groundspeak.com/cache/1/0"
>
<xsl:output method="html"/>
<xsl:template match="/">
Cache names:
<xsl:apply-templates select="//wpt">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="wpt">
<li><xsl:value-of select="groundspeak:cache/groundspeak:name"/></li>
</xsl:template>
</xsl:stylesheet>
And what I would expect is a list with one element on it, "Craft Maker Box", but what I get is an empty list.
What am I missing?
The default namespace is http://www.topografix.com/GPX/1/0. You should add that and use a prefix to match wpt.
Something like this: (untested)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:groundspeak="http://www.groundspeak.com/cache/1/0"
xmlns:gpx="http://www.topografix.com/GPX/1/0"
>
<xsl:output method="html"/>
<xsl:template match="/">
Cache names:
<xsl:apply-templates select="//gpx:wpt">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="gpx:wpt">
<li><xsl:value-of select="groundspeak:cache/groundspeak:name"/></li>
</xsl:template>
</xsl:stylesheet>
It is indeed a namespace issue. You have
xmlns="http://www.topografix.com/GPX/1/0"
in the XML so unprefixed element names are in this namespace. You need to bind the same uri to a prefix in your stylesheet, e.g.
xmlns:g="http://www.topografix.com/GPX/1/0"
and then use g:wpt in the match and select expressions.
I just can't figure out a way to output string something like :
<xml version="1.0" encoding="UTF-8">
this is what i tried:
<xsl:variable name="lessThan" select="<"/>
<xsl:variable name="GreaterThan" select=">"/>
<xsl:value-of select="$lessThan"/>
<xsl:text>xml version="1.0" encoding="UTF-8"</xsl:text>
<xsl:value-of select="$GreaterThan"/>
but this is the output i'm getting:
<xml version="1.0" encoding="UTF-8">
I also tried doin something like this:
<xsl:text><xml version="1.0" encoding="UTF-8"></xsl:text>
but the editor simply doesn't let me do this.It throws an error to match with end tag
PS:I am not well versed in xslt so Do please reply even if the question sounds naive.
try this:
<xsl:text disable-output-escaping="yes"><xml version="1.0" encoding="UTF-8"></xsl:text>
To make your test xslt working you can use disable-output-escaping = "yes"
Changed xlst:
<xsl:variable name="lessThan" select="'<'"/>
<xsl:variable name="GreaterThan" select="'>'"/>
<xsl:value-of disable-output-escaping = "yes" select="$lessThan"/>
<xsl:text>xml version="1.0" encoding="UTF-8"</xsl:text>
<xsl:value-of disable-output-escaping = "yes" select="$GreaterThan"/>
Update:
Only a guess you try to generate a xml declaration.
<?xml version="1.0" encoding="utf-8"?>
This should be done with xsl:output
<xsl:output method="xml" encoding="utf-8"/>
You should not be trying to produce the XML declaration manually. It should be generated automatically by the XSLT as long as you specify the output method as XML and do not specify omit-xml-declaration="yes":
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<xsl:template match="/">
<root />
</xsl:template>
</xsl:stylesheet>
When this XSLT is run on any input, the result is:
<?xml version="1.0" encoding="utf-8"?>
<root />
Put this <xsl:text disable-output-escaping="yes"><</xsl:text>
I'm sure this is a very simple fix, but I'm stumped. I've got input XML with the following root element, and repeating child elements:
<modsCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.loc.gov/mods/v3"
xsi:schemaLocation="
http://www.loc.gov/mods/v3
http://www.loc.gov/standards/mods/v3/mods-3-4.xsd">
<mods version="3.4">
...
I've got an XSLT sheet with the following to match each <mods> node, and kick it out as a separate file named by an <identifier type="local"> element.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.loc.gov/mods/v3">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/modsCollection">
<xsl:for-each select="mods">
<xsl:variable name="filename"
select="concat(normalize-space(
identifier[#type='local']),
'.xml')" />
<xsl:result-document href="{$filename}">
<xsl:copy-of select="."></xsl:copy-of>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This works if the XML input does not have the xmlns:xsi, xmlns, or xsi:schemaLoaction attributes in the root element. So, for example, it works on the following:
<modsCollection>
<mods version="3.4">
...
I know that some of our MODS files have had the prefix included but I'm unclear why this won't work without the prefix if our XSLT matching is not looking for the prefix. Any thoughts or advice would be greatly appreciated.
<xsl:template match="/modsCollection">
matches modsCollection in no namespace. You want
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.loc.gov/mods/v3"
xmlns:m="http://www.loc.gov/mods/v3">
then
<xsl:template match="/m:modsCollection">
To match modsCollection in the mods namespace, and similarly use the m: prefix in all xslt patterns and xpath expressions in the stylesheet.
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>