xml - Working out parent element name -


i have following xml fragment

<view>   <file>     <name>somefile_name</name>   </file> </view> <view> <view>   <directory>     <name>somedirectory_name</name>   </directory> </view> <view>   <pipe>     <name>somepipe_name</name>   </pipe> </view> 

and following xslt template

<xsl:template match="view" mode="view_mode" >   <xsl:if test=".//name" >     <data name="objectname">     <xsl:atribute name="value">       <!-- prefix object name type per            directory:somedirectory_name. each use of name() have tried            results in matching view element. xpath can use            gain name element's parent element name ie 'file', 'pipe',            or 'directory'       -->       <xsl:value-of select=".//name" />     </xsl:atribute>   </xsl:if> </xsl:template> 

my desired output, given input fragment above be

<data name="objectname" value="file:somefile_name" /> <data name="objectname" value="directory:somedirectory_name" /> <data name="objectname" value="pipe:somepipe_name" /> 

i have been trying work out xpath identify parent of name element in view_mode template above without success. can provide suggestions.

thanks in advance

assuming xslt 1.0, quick fix issue use:

<xsl:template match="view" mode="view_mode" >     <xsl:if test=".//name" >         <data name="objectname">             <xsl:attribute name="value">                 <xsl:value-of select="concat(name(.//name/..), ':', .//name)" />             </xsl:attribute>         </data>     </xsl:if> </xsl:template> 

however, rather awkward. more elegant solution match on name (as suggested @tirma), , use attribute value template:

<xsl:template match="name">     <data name="objectname" value="{concat(name(..), ':', .)}"/> </xsl:template> 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -