saxon:stream not getting the desired result
Added by Anonymous about 16 years ago
Legacy ID: #5235760 Legacy Poster: reshma (reshma_r)
I'am trying to use the saxon:stream function to process a large XML document but it does not return the desired output. The XML has the following structure <RootElem> <HdrElem> <HdrElemProp1 /> <HdrElemProp2 /> .... </HdrElem> <BdyElem> <Item Id='1' > <SubItem1> <SubSubItem1>value1</SubSubItem1> <SubSubItem2>value2</SubSubItem2> .... </SubItem1> </Item> <Item Id='2' > <SubItem1> <SubSubItem1>value3</SubSubItem1> <SubSubItem2>value4</SubSubItem2> .... </SubItem1> </Item> <Item> ..... </BdyElem> </RootElem> I need to transform this to a txt file that will have the following output HdrElemProp1 ~ HdrElemProp2 // xpath is \RootElem\HdrElem\HdrElemProp1 ~ \RootElem\HdrElem\HdrElemProp2 1 ~ value1 2 ~ value3 // xpath is \RootElem\BdyElem\Item[@Id] ~ \RootElem\BdyElem\Item\SubItem1\SubSubItem1 for each item element in body. The XSLT is <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/" > <xsl:output omit-xml-declaration="no" method="text" indent="yes" /> <xsl:template name="main" match="/RootElem/HdrElem" > <xsl:value-of select="HdrElemProp1"/> ~ <xsl:value-of select="HdrElemProp2"/> ~ </xsl:template> <xsl:template name="test" > <xsl:for-each select="saxon:stream(doc('')//RootElem/BdyElem/Item)" > <xsl:value-of select="@Id" /> ~ <xsl:value-of select="SubItem1/SubSubItem1 " />~ </xsl:for-each> </xsl:template> </xsl:stylesheet>
Replies (4)
Please register to reply
RE: saxon:stream not getting the desired result - Added by Anonymous about 16 years ago
Legacy ID: #5235781 Legacy Poster: reshma (reshma_r)
I missed mentioning that this always dumps the entire Item node in the output text file. Is this XSL correct ? I'm not able to use a match element with the template and have to use the complete path with the saxon:stream function. Is this understanding correct? I'm a newbie to using Saxon-Sa so please bear with me and help me with these simple queries Thanks in Advance Reshma.
RE: saxon:stream not getting the desired result - Added by Anonymous about 16 years ago
Legacy ID: #5236144 Legacy Poster: Michael Kay (mhkay)
When you process a document in streaming mode, your logic often needs to be designed differently to make this possible. In particular, your call on stream() needs to select everthing from the input that you need to use, because you only want to process the input once and once you have skipped something, there is no second chance. So in this case you will need a union expression: <xsl:template name="main"> <xsl:apply-templates select="stream(doc('in.xml')/RootElem/(HdrElem | BdyElem/Item)"/> </xsl:template> <xsl:template match="HdrElem"> ... </xsl:template> <xsl:template match="Item"> ... </xsl:template> >this always dumps the entire Item node in the output text file I suspect that you are running the transformation with the input file supplied both as the "principal input" and also as the input to the stream() function. You don't want to do this. The principal input of the transformation is always built as a tree in memory, it is never streamed. If there is only a single input to the transformation, and you want to process it in streaming mode, then you should run the transformation with no principal input, starting by nominating a named template ("main", in my example) as the entry point, and that named template should read the input file within a call to saxon:stream(). I hope this helps. Michael Kay Saxonica
RE: saxon:stream not getting the desired result - Added by Anonymous about 16 years ago
Legacy ID: #5241606 Legacy Poster: reshma (reshma_r)
Thanks a lot Michael! I've understood the difference as you are suggesting and your XSL file gets the required output. Could you please recommend some links where I can read and understand more on writing XSLT for processing xml in streaming mode. Thanks and Regards, Reshma.
RE: saxon:stream not getting the desired result - Added by Anonymous about 16 years ago
Legacy ID: #5241883 Legacy Poster: Michael Kay (mhkay)
>Could you please recommend some links where I can read and understand more on writing XSLT for processing xml in streaming mode. I'm afraid this is still a fairly new "art", so there's not much available other than the documentation at http://www.saxonica.com/documentation/sourcedocs/serial.html. Searching the list archives at http://saxon-help.markmail.org/ for "streaming" also gives a few hints and tips. (Unfortunately, searching the archives of this forum is not so easy - which is one reason I prefer people to use the mailing list if possible.)
Please register to reply