You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Dvir Ofek <dv...@ScreenPeaks.com> on 2002/10/17 17:58:27 UTC
How to transform a document fragment
Hi everybody
I was hopping you could assist me with my following problem - I need to
transform document fragments instead of a whole document.
What actually happens is that I have an xml document that looks like that:
<root>
<StyleSheet Name="a1"/>
<x:element1>blah blah</x:element1>
<StyleSheet Name="b"/>
<y:element1>blah blah</y:element1>
<StyleSheet Name="a2"/>
<x:element1>blah blah</x:element1>
<y:element1>blah blah</y:element1>
</root>
I need to transform this document using the style-sheets referenced in the
<StyleSheet> element. In the example - "a1" knows how to transform elements
from namespace x, and "b" knows how to transform elements from namespace y.
Later, I switch to another style-sheet instead of "a1", meaning "a2", which
also corresponds to elements from namespace x.
Therefore, my intention was to parse the document (using SAX2) and maintain
a map of compiled style-sheets; Every time I see a <StyleSheet> element I
add it to the map, or override the previous one (like the case of "a2"
substituting "a1"). Then, whenever I get a startElement() event I build a
DOM node, and when I get an endElement() event I transform that DOM Node
using all the compiled style-sheets in my map.
The bottom line is I could not find a function that can transform only a
document fragment, only whole parsed documents.
If any of you knows about a function that does it, or have an idea for a
better implementation, I'd be more than glad to hear about it.
Thanks in advance.
Cheers
Dvir Ofek
Software Engineer
Screenpeaks Ltd.
Re: How to transform a document fragment
Posted by Jon Blake <jo...@lw-oasis.org>.
Perhaps I'm missing something , but why not something like this in the
transform:
<xsl:template match="/root">
<xsl:apply-templates select="StyleSheet[@Name='a1']" />
</xsl:template>
. . .
Just have different transformations for each type of stylesheet, or even
pass in an XSL parameter of the stylesheet element name that you want
tranformed and have all of the tranformations in one XSL file.
<xsl:param name="stylesheetName">
<xsl:text>undefined-name</xsl:text>
</xsl:param>
<xsl:template match="/root">
<xsl:apply-templates select="StyleSheet[@Name='$stylesheetName']" />
</xsl:template>
. . .
Using modes would help separate the different transformations.
--
Jon Blake
On Thu, 17 Oct 2002, Dvir Ofek wrote:
> Hi everybody
> I was hopping you could assist me with my following problem - I need to
> transform document fragments instead of a whole document.
> What actually happens is that I have an xml document that looks like that:
>
> <root>
> <StyleSheet Name="a1"/>
> <x:element1>blah blah</x:element1>
> <StyleSheet Name="b"/>
> <y:element1>blah blah</y:element1>
> <StyleSheet Name="a2"/>
> <x:element1>blah blah</x:element1>
> <y:element1>blah blah</y:element1>
> </root>
>
> I need to transform this document using the style-sheets referenced in the
> <StyleSheet> element. In the example - "a1" knows how to transform elements
> from namespace x, and "b" knows how to transform elements from namespace y.
> Later, I switch to another style-sheet instead of "a1", meaning "a2", which
> also corresponds to elements from namespace x.
> Therefore, my intention was to parse the document (using SAX2) and maintain
> a map of compiled style-sheets; Every time I see a <StyleSheet> element I
> add it to the map, or override the previous one (like the case of "a2"
> substituting "a1"). Then, whenever I get a startElement() event I build a
> DOM node, and when I get an endElement() event I transform that DOM Node
> using all the compiled style-sheets in my map.
> The bottom line is I could not find a function that can transform only a
> document fragment, only whole parsed documents.
> If any of you knows about a function that does it, or have an idea for a
> better implementation, I'd be more than glad to hear about it.
> Thanks in advance.
>
> Cheers
> Dvir Ofek
> Software Engineer
> Screenpeaks Ltd.
>
>
Re: How to transform a document fragment
Posted by David N Bertoni/Cambridge/IBM <da...@us.ibm.com>.
>From the description of your processing model, I suspect you want to use an
instance of XalanDocument builder to create a separate source tree for each
sub-tree in the original document. Take a look at the DocumentBuilder
sample, which shows how you can use this class to build a tree from SAX2
events.
Dave
Dvir Ofek
<dvir@ScreenPeak To: "Xalan-C-Users@Xml. Apache. Org (E-mail)" <xa...@xml.apache.org>
s.com> cc: (bcc: David N Bertoni/Cambridge/IBM)
Subject: How to transform a document fragment
10/17/2002 08:58
AM
Hi everybody
I was hopping you could assist me with my following problem - I need to
transform document fragments instead of a whole document.
What actually happens is that I have an xml document that looks like that:
<root>
<StyleSheet Name="a1"/>
<x:element1>blah blah</x:element1>
<StyleSheet Name="b"/>
<y:element1>blah blah</y:element1>
<StyleSheet Name="a2"/>
<x:element1>blah blah</x:element1>
<y:element1>blah blah</y:element1>
</root>
I need to transform this document using the style-sheets referenced in the
<StyleSheet> element. In the example - "a1" knows how to transform elements
from namespace x, and "b" knows how to transform elements from namespace y.
Later, I switch to another style-sheet instead of "a1", meaning "a2", which
also corresponds to elements from namespace x.
Therefore, my intention was to parse the document (using SAX2) and maintain
a map of compiled style-sheets; Every time I see a <StyleSheet> element I
add it to the map, or override the previous one (like the case of "a2"
substituting "a1"). Then, whenever I get a startElement() event I build a
DOM node, and when I get an endElement() event I transform that DOM Node
using all the compiled style-sheets in my map.
The bottom line is I could not find a function that can transform only a
document fragment, only whole parsed documents.
If any of you knows about a function that does it, or have an idea for a
better implementation, I'd be more than glad to hear about it.
Thanks in advance.
Cheers
Dvir Ofek
Software Engineer
Screenpeaks Ltd.