0

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 &gt; '2001-01-01' ">
Good job
</xsl:if>

What's wrong ?

thanks

3
  • 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 &gt;), and I change my date to be higher and lower, and each time it doesn't work <xsl:if test="rdf:Description/dcterms:issued &gt; '2001-01-01' "> Good job </xsl:if> What's wrong ? Commented Dec 23, 2013 at 16:51
  • If the data you're processing is RDF then it would be much safer to process it using an RDF library rather than XML tools - there are many syntactically different but semantically equivalent ways to represent the same RDF graph in XML, and you may find your transformation stops working if you round-trip the RDF through a tool that uses a different representation from the one you expected. Commented Dec 23, 2013 at 17:07
  • thanks Ian. But finally I found something saying that I can't validate date. So I try this :<xsl:if test="number(substring(rdf:Description/dcterms:issued,1,4)) &gt;= 2002 and number(substring(rdf:Description/dcterms:issued,6,2)) &gt;= 1 and number(substring(rdf:Description/dcterms:issued,9,2)) &gt;= 10"> And I can validate now my date Commented Dec 23, 2013 at 20:33

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Tim , I try your code and it works. I will now take some minutes to cleary understanding this code. THANKS A LOT !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.