home contribute faq download

FunctX XSLT Functions

functx:dynamic-path

Dynamically evaluates a simple XPath path

Google
Webxsltfunctions.com

Description

The functx:dynamic-path function dynamically evaluates a simple path expression. The function only supports element names and attribute names preceded by @, separated by single slashes. The names can optionally be prefixed, but they must use the same prefix that is used in the input document. It does not support predicates, other axes or other node kinds. Note that most processors have an extension function that evaluates path expressions dynamically in a much more complete way.

Arguments and Return Type

NameTypeDescription
$parent node() the root to start from
$path xs:string the path expression
return value item()*

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:dynamic-path" as="item()*"
              xmlns:functx="http://www.functx.com">
  <xsl:param name="parent" as="node()"/>
  <xsl:param name="path" as="xs:string"/>

  <xsl:variable name="nextStep"
        select="functx:substring-before-if-contains($path,'/')"/>
  <xsl:variable name="restOfSteps"
        select="substring-after($path,'/')"/>
  <xsl:for-each select="
    ($parent/*[functx:name-test(name(),$nextStep)],
     $parent/@*[functx:name-test(name(),
                              substring-after($nextStep,'@'))])">
    <xsl:variable name="child" select="."/>
    <xsl:sequence select="if ($restOfSteps)
         then functx:dynamic-path($child, $restOfSteps)
         else $child"/>
  </xsl:for-each>

</xsl:function>

Examples

<xsl:variable name="in-xml" as="item()*">
<authors>
   <author test="abc">
      <first>Kate</first>
      <last>Jones</last>
   </author>
   <author>
      <first>John</first>
      <a:last xmlns:a="http://a">Doe</a:last>
   </author>
</authors>
</xsl:variable>
XPath ExampleResults
functx:dynamic-path(
     $in-xml,'author/first')
<first>Kate</first>
<first>John</first>
name(functx:dynamic-path(
     $in-xml,'author/@test'))
test
functx:dynamic-path(
     $in-xml,'author')
<author test="abc">
  <first>Kate</first>
  <last>Jones</last>
</author>
<author>
  <first>John</first>
  <a:last xmlns:a="http://a">Doe</a:last>
</author>
functx:dynamic-path(
     $in-xml,'author/a:last')
<a:last xmlns:a="http://a">Doe</a:last>

Depends On

functx:substring-before-if-containsPerforms substring-before, returning the entire string if it does not contain the delimiter
functx:name-testWhether a name matches a list of names or name wildcards

See Also

functx:path-to-nodeA path to an XML node (or sequence of nodes)

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Datypic XSLT Services

Recommended Reading:

XQuery