RE: [xsl] create xml from incoming xml and load it into a variable

Subject: RE: [xsl] create xml from incoming xml and load it into a variable
From: sudheshna iyer <sudheshnaiyer@xxxxxxxxx>
Date: Mon, 28 Jul 2008 03:57:15 -0700 (PDT)
Thank you for the reply.

I have the following xsl file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xalan="http://xml.apache.org/xalan";>
	<xsl:key name="keyname" match="subroot" use="ccc"/>
	<xsl:template match="root">
		<xsl:variable name="var">
			<xsl:element name="root">
				<xsl:element name="subroot">
					<xsl:apply-templates select="file_entry"/>
				</xsl:element>
			</xsl:element>
		</xsl:variable>
		<!--Traverse the variable-->
		<xsl:for-each select="xalan:nodeset($var)/root">
			<xsl:variable name="test" select="key('keyname', '11')"/>
			<xsl:text>inside root.....: </xsl:text>

			<xsl:for-each select="$test">	  		==============> Why is this test var empty???
				<xsl:text>inside test.....: </xsl:text>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="subroot">
		<xsl:element name="subroot">
			<xsl:element name="ccc">
				<xsl:apply-templates select="ccc" mode="copy"/>
			</xsl:element>
			<xsl:element name="ddd">
				<xsl:apply-templates select="ddd" mode="copy"/>
			</xsl:element>
			<xsl:element name="eee">
				<xsl:apply-templates select="eee" mode="copy"/>
			</xsl:element>
		</xsl:element>
	</xsl:template>	
</xsl:stylesheet>

For the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>    
    <subroot id="11111">
        <ccc>11</ccc>
        <ddd>2005-08-26</ddd>
        <eee>aaaaaaaa</eee>
    </subroot>
    <subroot id="11111">
        <ccc>22</ccc>
        <ddd>2005-08-26</ddd>
        <eee>aaaaaaaa</eee>
    </subroot>
    <subroot id="11111">
        <ccc>11</ccc>
        <ddd>2005-08-26</ddd>
        <eee>aaaaaaaa</eee>
    </subroot>
</root> 

I put the key in xsl:sheet and called where I need. But I am not sure why test variable is empty?
Shouldn't it contain two subroots that match from the above xml which has 3 subroots?

    <subroot id="11111">
        <ccc>11</ccc>
        <ddd>2005-08-26</ddd>
        <eee>aaaaaaaa</eee>
    </subroot>
    <subroot id="11111">
        <ccc>11</ccc>
        <ddd>2005-08-26</ddd>
        <eee>aaaaaaaa</eee>
    </subroot>    




--- On Fri, 7/25/08, Michael Kay <mike@xxxxxxxxxxxx> wrote:

> From: Michael Kay <mike@xxxxxxxxxxxx>
> Subject: RE: [xsl] create xml from incoming xml and load it into a variable
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Date: Friday, July 25, 2008, 4:57 PM
> Well, you can't use xsl:key anywhere other other than as
> a child of
> xsl:stylesheet.
> 
> How to correct your code depends rather on what you thought
> your code was
> intended to mean. I suspect you want the xsl:key as
> written, but moved to
> the right place, and a call on the key() function where you
> currently have
> the xsl:key element.
> 
> Remember that an xsl:key declaration is not tied to a
> specific document or
> tree. It's the call on key() that determines which tree
> will be searched.
> 
> Michael Kay
> http://www.saxonica.com/ 
> 
> > -----Original Message-----
> > From: sudheshna iyer [mailto:sudheshnaiyer@xxxxxxxxx] 
> > Sent: 25 July 2008 21:37
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > Subject: Re: [xsl] create xml from incoming xml and
> load it 
> > into a variable
> > 
> > Thank you.
> > 
> > I did this using:
> > 	<xsl:template match="root">
> > 	  <xsl:variable name="var">
> > 	    <xsl:element name="subroot"
> use-attribute-sets="id">
> > 		<xsl:apply-templates select="bbb"/>
> > 	    </xsl:element>
> > 	  </xsl:variable>
> > 	  
> > 	  <!--Traverse the variable-->
> > 	  <xsl:for-each
> select="xalan:nodeset($var)/root/subroot">
> > 	    <xsl:key name="ccc"
> match="subroot" use="11"/> ===> 
> > ERROR!!!!
> > 	  </xsl:for-each>
> > 	</xsl:template>
> > 
> > Sample xml:
> > <?xml version="1.0"
> encoding="UTF-8"?>
> > <root>    
> >     <subroot id="11111">
> >         <ccc>11</ccc>
> >         <ddd>2005-08-26</ddd>
> >         <eee>aaaaaaaa</eee>
> >     </subroot>
> >     <subroot id="11111">
> >         <ccc>22</ccc>
> >         <ddd>2005-08-26</ddd>
> >         <eee>aaaaaaaa</eee>
> >     </subroot>
> >     <subroot id="11111">
> >         <ccc>11</ccc>
> >         <ddd>2005-08-26</ddd>
> >         <eee>aaaaaaaa</eee>
> >     </subroot>
> > </root> 
> > 
> > Inside the loop, I need to use  <xsl:key
> name="ccc" 
> > match="subroot" use="ccc"/>. 
> > But while transforming, I am receiving the following
> error:
> > 
> > xsl:key is not allowed in this position in the
> stylesheet!
> > 
> > How do I use "key" tag inside a variable
> which contains xml?
> > 
> > I appreciate your help.
> > 
> > 
> > --- On Fri, 7/25/08, Andrew Welch
> <andrew.j.welch@xxxxxxxxx> wrote:
> > 
> > > From: Andrew Welch
> <andrew.j.welch@xxxxxxxxx>
> > > Subject: Re: [xsl] create xml from incoming xml
> and load it into a 
> > > variable
> > > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > > Date: Friday, July 25, 2008, 6:02 AM
> > > 2008/7/25 sudheshna iyer
> <sudheshnaiyer@xxxxxxxxx>:
> > > > How do I create xml from incoming xml and
> load it into
> > > a variable from xsl? Afterr loading I
> > > >
> > > > have to loop through the variable for doing
> more xsl
> > > transformations?
> > > >
> > > > My xsl has incoming xml. I have to create
> another xml
> > > of different format, put it in a variable
> > > > and loop through that variable to apply more
> xsl
> > > transformations.
> > > 
> > > 
> > > Do the first pass in a variable:
> > > 
> > > <xsl:variable name="first-pass">
> > >   <xsl:apply-templates
> mode="first-pass"/> </xsl:variable>
> > > 
> > > The do the second pass by calling apply-templates
> on the first pass:
> > > 
> > > <xsl:apply-templates
> select="$first-pass"/>
> > > 
> > > ...or something similar - you may want to mode
> the second 
> > pass if it's 
> > > less work.  Remember to have two root match
> templates, one 
> > moded, to 
> > > ensure do don't get infinite recursion.
> > > 
> > > If the first pass could be a one-off batch
> process, then 
> > you're better 
> > > off separating out the transforms than repeatedly
> doing the 
> > first pass 
> > > each time.
> > > 
> > > --
> > > Andrew Welch
> > > http://andrewjwelch.com
> > > Kernow: http://kernowforsaxon.sf.net/

Current Thread