Wednesday 15 April 2015

XPath/XSLT 1.0: Identifying element based on a sub element value -



XPath/XSLT 1.0: Identifying element based on a sub element value -

i developing xslt translation can utilize xslt 1.0 xpath 1.0. let's assume have next xml , want select object original_file_name attribute ends ".prt". how can this?

<object> <object> <attribute name="original_file_name">file1.qaf</attribute> <attribute name="otherattribute1">val</attribute> <attribute name="otherattribute2">val</attribute> </object> <object> <attribute name="original_file_name">file2.tso</attribute> <attribute name="otherattribute1">second guess</attribute> <attribute name="otherattribute2">val</attribute> </object> <object> <attribute name="original_file_name">file3.prt</attribute> <attribute name="otherattribute1">first guess!</attribute> <attribute name="otherattribute2">val</attribute> </object> </object>

for xslt 2.0, have found solution, can't rid of index-of not supported in xpath 1.0. have hints solution?

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fo="http://www.w3.org/1999/xsl/format" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:output method="xml" encoding="iso-8859-1" indent="yes"/> <xsl:template match="/object"> <xsl:variable name="fileindex"> <xsl:call-template name="identifyfile"> <xsl:with-param name="filenames" select="object/attribute[@name='original_file_name']"/> </xsl:call-template> </xsl:variable> <xsl:variable name="fileobj" select="object[position()=$fileindex]"/> <!-- sth. fileobj --> <xsl:copy-of select="$fileobj" /> </xsl:template> <xsl:template name="identifyfile"> <xsl:param name="filenames"/> <xsl:choose> <!-- add together other file extensions here --> <xsl:when test="$filenames['.prt'=substring(., string-length() - 3)]"> <xsl:value-of select="index-of($filenames, $filenames['.prt'=substring(., string-length() - 3)])" /> </xsl:when> <xsl:when test="$filenames['.tso'=substring(., string-length() - 3)]"> <xsl:value-of select="index-of($filenames, $filenames['.tso'=substring(., string-length() - 3)])" /> </xsl:when> <!-- if no known file extension has been found, take default one. --> <xsl:otherwise> <xsl:value-of select="1"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>

edit: simplified problem much in first step :). have added 2nd file extension should searched in case first 1 not found. if in illustration no '.prt'-file, '.tso'-file should taken.

if want select based on status xpath look

object[attribute[@name='original_file_name' , substring(., string-length() - 3) = '.prt']]

in copy-of should suffice. edited sample think want

<xsl:template match="/object"> <xsl:variable name="filenames" select="object/attribute[@name='original_file_name']"/> <xsl:choose> <!-- add together other file extensions here --> <xsl:when test="$filenames['.prt'=substring(., string-length() - 3)]"> <xsl:copy-of select="$filenames['.prt'=substring(., string-length() - 3)]" /> </xsl:when> <xsl:when test="$filenames['.tso'=substring(., string-length() - 3)]"> <xsl:copy-of select="$filenames['.tso'=substring(., string-length() - 3)]" /> </xsl:when> <!-- if no known file extension has been found, take default one. --> <xsl:otherwise> <xsl:copy-of select="$filenames[1]"/> </xsl:otherwise> </xsl:choose> </xsl:template>

xslt xpath

No comments:

Post a Comment