xslt - Wrapping my head around effecient solutions to copy-pasting chunks of code -


coming language php, naturally hate copy , pasting code on , on changing single variable or value. have series of code chunks in xslt copy , pasted on , on looking specific node value, , doing something.

i start chunk, uses populate list of names each function:

<xsl:variable name="<!-- variable of names -->">     <xsl:for-each select="//<!-- functions -->">         <xsl:for-each select="./<!-- user functions -->">             <xsl:if test="<!-- specific function -->">             <xsl:value-of select="title"/>             <xsl:text> </xsl:text>             <xsl:value-of select="firstname"/>             <xsl:text> </xsl:text>             <xsl:value-of select="lastname"/>         </xsl:for-each>     </xsl:for-each> </xsl:variable> 

here logic use grab:

<xsl:if test="<!-- specific list of users have function happen them -->">     <xsl:call-template name="<!-- specific template -->">         <xsl:with-param name="listofnames">             <xsl:value-of select="<!-- function grabs list of names -->" disable-output-escaping="yes"/>         </xsl:with-param>     </xsl:call-template> </xsl:if> 

later use following chunk of code display results:

<xsl:template name="<!-- specific template -->">     <xsl:param name="listofnames"/>     <!-- specific stuff --> </xsl:template> 

i have wracked brain looking @ , can't head around way of doing more recursively. or @ least stop me copy , pasting same 3 chunks of code every function (of there 18)...

i know isn't "post code , sorted" board think there better way god me can't see it.

here example of xml part of xslt works with:

<userupdated>     <userdetails>         <title>mr</title>         <firstname>test</firstname>         <lastname>evo</lastname>     </userdetails> </userupdated> <flightservicingfunction>functionname</flightservicingfunction> <flightservicingfunction>functionname</flightservicingfunction> <flightservicingfunction>.... (continues per function has done user) 

let me know if need more information. in advance , help.

here example: http://xsltransform.net/ppqshsz

this xslt typically produced people learned syntax not concepts. typical pull-style produced people know input be, , mindset automatically pull data it.

the proper way use xslt oblivious input possible, , specifiy templates specific tags care about. push style transformation. current xslt relies on xsl:for-each loop on input tags , use xsl:if see if tag matches want.

instead, should use xsl:apply-templates generic select (using lot of wildcards) in combination multiple xsl:templates, each specific match.

since didn't specify exact input or output, here's concept should started:

<xsl:template match="/">     <xsl:apply-templates select="//flightservicingfunction" /> </xsl:template>  <xsl:template match="flightservicingfunction[. = '<!--specific function-->']">     <xsl:value-of select="title"/>     <xsl:text> </xsl:text>     <xsl:value-of select="firstname"/>     <xsl:text> </xsl:text>     <xsl:value-of select="lastname"/> </xsl:template> 

you'd still have copy-paste xsl:template match="..." bit couple of times, rid of aggressive looping way in order make xslt processor you. designed for.

the trick getting rest of data (the stuff put in variables) conquer relative position in nodeset matching , use xpath select it. provides ample tools want, whatever active node is.

further suggested reading: http://www.usingxml.com/transforms/xslidentity. luck!


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -