Copied to clipboard

Flag this post as spam?

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


  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Mar 06, 2015 @ 17:44
    Damiaan
    0

    Get 2 different document types using xpath

    I need to filter out 2 document types on an xpath-based datatype.

    $currentPage/ancestor-or-self::*[@level=4]/child::*[name()='DocType1']
    

    But if i change this to

    $currentPage/ancestor-or-self::*[@level=4]/child::*[name()='DocType1' or name()='DocType2']
    

    I still only get only docType1 childs. What am I doing wrong?

    I don't think it matters, but it's an old umbraco 4.10.11.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 06, 2015 @ 17:53
    Jan Skovgaard
    0

    Hi Damiaan

    I think you should be able to actually do this

    <xsl:variable name="$currentPage/ancestor-or-self::*[@level=4]/DocType1|$currentPage/ancestor-or-self::*[@level=4]/DocType2" />
    

    Hope this helps.

    /Jan

  • Sebastian Dammark 581 posts 1385 karma points
    Mar 07, 2015 @ 15:14
    Sebastian Dammark
    1

    Or you could do something like this

    <xsl:template match="/">
        <xsl:apply-templates select="$currentPage/ancestor-or-self::*[@level=4]/*" />
    </xsl:template>
    
    <xsl:template match="*[name()='DocType1']">
    Do stuff for Doctype 1 here
    </xsl:template>
    
    <xsl:template match="*[name()='DocType2']">
        Do stuff for Doctype 2 here
    </xsl:template>
    
    <xsl:template match="*" />
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 07, 2015 @ 17:26
    Chriztian Steinmeier
    2

    Hi Damiaan,

    What you're doing is technically correct - you can shorten the XPath by doing a couple of things, but as it is, it should do exactly what you want; so if you're only getting DocType1 nodes in the result, here's why:

    1. The DocType2 nodes are not actually called DocType2? (XML is case-sensitive, so could be any combination, e.g. Doctype2 or docType2)
    2. The DocType2 nodes are unpublished? - The XML cache only contains published content, so no chance of getting unpublished content with XSLT
    3. The DocType2 nodes are not located at the same level as the DocType1 nodes?
    4. In your specific case: Pure Magic? :-)

    Okay - so far so good; there's another thing that could trip you up: Let's say you've selected the nodes and put them in a variable, and you just want to test whether you got the nodes or not, so you try this:

    <xsl:value-of select="$nodes" />
    

    ...thinking that you should get some sort of combined output, right? Wrong - value-of will print the string-result of a single node, but you're feeding it a set of nodes, so it'll print the string() version of the first node in the set. If that's a DocType1 node, you'll see that and think "where's my DocType2 node?"

    So if you're testing, you'd better use count() to make sure, e.g.:

    <xsl:value-of select="count($nodes)" />
    

    ...and you'd see whether you had only a single node or you'd actually managed to select more than one.

    Even better though, as Sebastian was hinting at, use match templates to verify the result, e.g.:

    <xsl:apply-templates select="$nodes" />
    
    <xsl:template match="*[@isDoc]">
        <p>My name is <xsl:value-of select="name()" /></p>
    </xsl:template>
    

    (Using a generic template will catch the case where you've accidentally camelCased the name - using exact matches would trip those results up)

    Hope you have enough tools for debugging this now :-)

    /Chriztian

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Mar 09, 2015 @ 22:06
    Damiaan
    0

    I'll try it tomorrow! Thanks for all your comments!

Please Sign in or register to post replies

Write your reply to:

Draft