cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joerg Heinicke <joerg.heini...@gmx.de>
Subject Re: Problem with xsl parsing xhtml
Date Thu, 16 Jan 2003 17:54:55 GMT
Hello Anna,

Anna Afonchenko wrote:
> Thank you very much Joerg, this worked!!!
> But now I have a more complex pattern, and it doesn't work again.
> I want to have a match that will say this:
> for each node, that has a child div node with class name test, copy to the
> output only the content of the div.
> So, for
> <p>
>     <div class="test">
>         <b>Text inside div</b>
>    </div>
> </p>
> 
> the output should be
> <b>Text inside div</b>
> 
> This is the template that I wrote:
> <xsl:template
> match="html:node()[child::node()[name()='div'][@class='test']]">
>    <xsl:apply-templates select="descendant::html:div/html:node()"/>
>  </xsl:template>

name() returns the namespace-prefix plus the element's name, e.g. 
"html:div". In the above code [name()='div'] won't be true, so the 
template does not match. One solution is to change it to 
[name()="html:div"], a second one to [local-name()="div"], but both are 
not really namespace aware. In my eyes the best solution is to use 
[self::html:div].

But the match can simplified:

<xsl:template match="html:*[html:div[@class='test']]">
   <xsl:apply-templates select="html:div/node()"/>
</xsl:template>

And if you want to remove <div class="test"/> in general, I think a template

<xsl:template match="html:div[@class='test']">
   <xsl:apply-templates select="node()"/>
</xsl:template>

is the best solution.

Regards,

Joerg

> This works (without html: namespace) if the input doesn't have xhtml doctype
> and xmlns.
> But when I put those, the output is not affected, i.e. the nodeset above is
> printed out as is.
> What am I doing wrong with namespaces this time?
> 
> Thank you very very much for help.
> Anna.
> 
> 
> ----- Original Message -----
> From: "Joerg Heinicke" <joerg.heinicke@gmx.de>
> To: <cocoon-users@xml.apache.org>
> Sent: Thursday, January 16, 2003 7:18 PM
> Subject: Re: Problem with xsl parsing xhtml
> 
> 
> Hello Anna,
> 
> all your elements in the XML are in the default namespace, so their
> complete name consists of the namespace-uri and the elment's name. If
> you want to match such an element, you must declare the namespace in the
> XSL. There is only one problem with default namespace: You can not
> simply declare it in the XSL, the matching won't work. You *must* bind
> it to a prefix. So changing your stylesheet to the following should
> solve your problem:
> 
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:html="http://www.w3.org/1999/xhtml">
> 
> <xsl:template match="node()|@*">
>    <xsl:copy>
>      <xsl:apply-templates select="node()|@*" />
>    </xsl:copy>
> </xsl:template>
> 
> <xsl:template match="html:link[@rel='stylesheet']"/>
> 
> </xsl:stylesheet>
> 
> Regards,
> 
> Joerg
> 
> Anna Afonchenko wrote:
> 
>>Hi all.
>>This is not really a Cocoon problem, but maybe you can help me with this.
>>I have the following XHTML page:
>><!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
>><html xmlns="http://www.w3.org/1999/xhtml">
>><head>
>><title>Page</title>
>><link href="pics/main_ns.css" rel="stylesheet" type="text/css" />
>></head>
>><body>
>>    <p>Some content</p>
>></body>
>></html>
>>
>>and I want to parse it and take out the link to external stylesheet.
>>So I wrote the following xsl:
>><?xml version="1.0" encoding="UTF-8"?>
>><xsl:stylesheet version="1.0"
>>xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>>
>><xsl:template match="node()|@*">
>><xsl:copy>
>><xsl:apply-templates select="node()|@*" />
>></xsl:copy>
>></xsl:template>
>>
>><xsl:template match="link[@rel='stylesheet']">
>></xsl:template>
>></xsl:stylesheet>
>>
>>Here is my pipeline:
>><map:match pattern="parseXhtml">
>><map:generate src="test.html"/>
>><map:transform src="disableCSS.xsl"/>
>><map:serialize type="xml"/>
>></map:match>
>>
>>This stylesheet should output the original page without link tag that
>>links to external stylesheet.
>>The problem is, it doesn't match, e.g. the output is exactly the
>>original input with the link tag left.
>>
>>If I take out the xhtml declaration from the input file, e.g. delete the
>>doctype declaration and xmlns attribute from the html tag, everything
>>works fine.
>>
>>Is there any prorblem in Cocoon/xsl while matching files with xhtml
>>declaration?
>>
>>Can somebody explain this to me.
>>
>>Sorry if this is a bit unrelated and thanks very much for help.
>>
>>Anna


---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <cocoon-users-unsubscribe@xml.apache.org>
For additional commands, e-mail:   <cocoon-users-help@xml.apache.org>


Mime
View raw message