Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • RJ 7 posts 27 karma points
    Nov 19, 2011 @ 22:08
    RJ
    0

    Select Nodes of a specific Document Type - Pass Document Type by Param

    I know how to select nodes of a specific document type, i.g. select nodes of document type 'News':

    <xsl:for-each select="$currentPage/ancestor-or-self::* [@isDoc]/News/* [@isDoc]">

     

    But... now I want to pass the name of the specific document type to the xslt file by Param. Something like:

    <xsl:param name="documentType" select="/macro/documentType" />
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@isDoc]/$documentType/* [@isDoc]">

    Unfortunately, I get a 'Unexpected token' error, I cannot use the $-sign.

    Any suggestions how to solve this?

    I'd really appreciate it!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 19, 2011 @ 22:56
    Chriztian Steinmeier
    1

    Hi RJ,

    You can use the name() function to perform that test/selection, e.g. to select all documents of a specific type:

    <xsl:variable name="documentType" select="/macro/documentType" />
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
    <xsl:for-each select="$siteRoot//*[name() = $documentType]">
       <!-- do stuff -->
    </xsl:for-each>

    /Chriztian

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Nov 19, 2011 @ 22:57
    Tom Fulton
    0

    Hi,

    You can use a variable for a document type alias by using the name() function:

    <xsl:for-each select="$currentPage/ancestor-or-self::* [@isDoc]/* [@isDoc][name() = $documentType]/* [@isDoc]">

    Hope this helps,
    Tom

  • RJ 7 posts 27 karma points
    Nov 20, 2011 @ 08:22
    RJ
    0

    Hello Chriztian, Tom,

    Indeed, the function name() did the trick. Thanks! Currently my select statement looks like this and it works:

    <xsl:variable name="newsDocumentType" select="'News'" />
    <xsl:variable name="activitiesDocumentType" select="'Activities'" />
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@isDoc]/* [name() = $newsDocumentType]/* [@isDoc] | $currentPage/ancestor-or-self::* [@isDoc]/* [name() = $activitiesDocumentType]/* [@isDoc]">

    Suggestions to make this shorter are welcome. The select statement should select all nodes that are either a News item or an Activity.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 20, 2011 @ 10:21
    Chriztian Steinmeier
    0

    Hi RJ,

    You can shorten that a bit by way of combining the name() tests inside the same predicate:

    <xsl:for-each select="$currentPage/ancestor-or-self::*[@isDoc]/*[name() = $newsDocumentType or name() = $activitiesDocumentType]/*[@isDoc]">

    That way, you're also getting rid of the duplication, which is a good thing(TM) 

    /Chriztian

  • RJ 7 posts 27 karma points
    Nov 20, 2011 @ 15:48
    RJ
    0

    Hello Chriztian,

    That was where I was looking for. Thanks! I already tried something like that, but I think I misused the β€˜|’-operator, instead of the β€˜or’-operator. Despite http://www.w3schools.com/xpath/xpath_operators.asp as a reference, I still do not fully understand what makes the difference between these two operators.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 20, 2011 @ 16:44
    Chriztian Steinmeier
    0

    Hi RJ,

    You can think of the pipe ( | ) as a join operator - it will join the nodes of two nodesets into a single nodeset; The or operator is just a boolean operation that works as you're used to in countless other languages. 

    /Chriztian

  • RJ 7 posts 27 karma points
    Nov 20, 2011 @ 17:15
    RJ
    0

    Hello Chriztian,

    Thanks for your explanation. It was a great help.

Please Sign in or register to post replies

Write your reply to:

Draft