Subject: Re: [xsl] add an attribute to an element, then use it in another template From: Pablo Sebastián Rodriguez <psrodriguez@xxxxxxxxxxxxxxxx> Date: Thu, 6 Mar 2008 00:19:53 -0300 (ART) |
the xml an xsl stuff i wrote here are not real things. just examples (apparently not very good ones ;-) ) what i'm really working on are templates that create php code, defining classes, populating objects, setting their properties, etc, etc... i can't put any of that here, too many lawyers around on this side of the screen... besides, i think it wouldn't make things clearer the trouble was not processing the attributes and their values. it was trying to add new ones. thanks to the people on this list, that's in the past.... xml: <music> <project name="Winterkalte"> <release name="Structures of Destruction" year="1997"/> <release name="Drum 'n' noise" year="1999"/> </project> </music> xsl: <xsl:template match="music"> <xsl:variable name="some_name" select="string('genre')"/> <xsl:variable name="some_value" select="string('noise')"/> <!-- here, with the as="element()" thing, the magic happens --> <xsl:variable name="another_stuff" as="element()*"> <xsl:element name="project"> <xsl:attribute name="{$some_name}"> <xsl:value-of select="$some_value"/> </xsl:attribute> </xsl:element> </xsl:variable> <xsl:call-template name="attr"> <xsl:with-param name="stuff" select="project|$another_stuff"/> </xsl:call-template> </xsl:template> <xsl:template name="attr"> <xsl:param name="stuff"/> <xsl:for-each select="$stuff/@*"> <xsl:text>attrName: </xsl:text> <xsl:value-of select="name()"/> <xsl:text> attrValue: </xsl:text> <xsl:value-of select="."/> <xsl:text> </xsl:text> </xsl:for-each> </xsl:template> output: attrName: name attrValue: Winterkalte attrName: genre attrValue: noise > look like your code work as expected - only one notice from me - it > better to use push model instead pull model (or data-driven instead > template-driven) > > try use something like <xsl:apply-templates select="bla-bla/@*" > mode="pairs"> > > <xsl:template match="@*" mode="pairs"> > attrName: <xsl:value-of select="name()"/> > attrValue: <xsl:value-of select="."/> > </xsl:template> > > On Wed, Mar 5, 2008 at 3:56 PM, Pablo Sebastian Rodriguez > <psrodriguez@xxxxxxxxxxxxxxxx> wrote: >> wow ! really nice trick ... not quite what i was looking for, but it'll >> get the job done, thanks. >> i think i wasn't clear enough >> >> with >> >> >> <music> >> >> <project name="W.A.S.T.E."> >> <release name="We all seek total entropy" year="2003"/> >> <release name="Violent delights" year="2006"/> >> </project> >> >> </music> >> >> i get >> >> attrName: name >> attrValue: W.A.S.T.E. >> >> >> if i had >> >> <music> >> >> <project name="W.A.S.T.E." newThing="qwertyu"> >> >> <release name="We all seek total entropy" year="2003"/> >> <release name="Violent delights" year="2006"/> >> </project> >> >> </music> >> >> the result would be >> >> >> attrName: name >> attrValue: W.A.S.T.E >> >> attrName: newThing >> attrValue: qwertyu >> >> >> thanks again >> >> >> >> > In XSLT 2.0 you can do: >> > >> > <xsl:template match="music"> >> > <xsl:call-template name="attr"> >> > <xsl:with-param name="stuff" select="project"/> >> > </xsl:call-template> >> > <xsl:call-template name="attr"> >> > <xsl:with-param name="stuff" as="element()*"> >> > <project newThing="whoKnowsWhat"/> >> > <project adadad="qwertyu"/> >> > </xsl:with-param> >> > </xsl:call-template >> > </xsl:template> >> > >> > Alternatively you can do a 2-phase transformation: phase one takes >> the >> > input >> > and adds a couple of attributes, phase 2 applies your current logic. >> > >> > Michael Kay >> > http://www.saxonica.com/ >> > >> >> hi, >> >> i'm new using xsl, so please be patient... ;-) >> >> >> >> the input is something like this: >> >> >> >> <music> >> >> >> >> <project name="W.A.S.T.E"> >> >> <release name="We all seek total entropy" year="2003"/> >> >> <release name="Violent delights" year="2006"/> >> >> </project> >> >> >> >> <project name="Winterkalte"> >> >> <release name="Structures of Destruction" year="1997"/> >> >> <release name="Drum 'n' noise" year="1999"/> >> >> </project> >> >> >> >> </music> >> >> >> >> >> >> the xsl: >> >> >> >> <xsl:template match="music"> >> >> <xsl:call-template name="attr"> >> >> <xsl:with-param name="stuff" select="project"/> >> >> </xsl:call-template> >> >> </xsl:template> >> >> >> >> <xsl:template name="attr"> >> >> <xsl:param name="stuff"/> >> >> <xsl:for-each select="$stuff/@*"> >> >> >> >> attrName: <xsl:value-of select="name()"/> >> >> attrValue: <xsl:value-of select="."/> >> >> >> >> </xsl:for-each> >> >> </xsl:template> >> >> >> >> the output: >> >> >> >> attrName: name >> >> attrValue: W.A.S.T.E >> >> >> >> attrName: name >> >> attrValue: Winterkalte >> >> >> >> >> >> what i'm trying to do is add an attribute so the output will be: >> >> >> >> attrName: newThing >> >> attrValue: whoKnowsWhat >> >> >> >> attrName: name >> >> attrValue: W.A.S.T.E >> >> >> >> attrName: name >> >> attrValue: Winterkalte >> >> >> >> attrName: adadad >> >> attrValue qwertyu >> >> >> >> i tried using <xsl:attribute> but i can't make it work... any ideas >> ? >> >> >> >> thanks
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] add an attribute to an el, Vyacheslav Sedov | Thread | Re: [xsl] add an attribute to an el, Florent Georges |
[xsl] two-level grouping, Terry Ofner | Date | [xsl] [XSLT] Put element in new-lin, igutierrez027 |
Month |