Copied to clipboard

Flag this post as spam?

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


  • Nasko26 1 post 21 karma points
    Mar 27, 2010 @ 01:44
    Nasko26
    0

    XSLT Custom filter function

    Hello,

    I need some help here, please.
    I want to implement a custom XSLT function to filter a list of people. It is supposed to make a simple comparison and output the ones matching the filter but it is not working properly.

    Thanks in advance!

    Here are my files:
    ----------
    person.xml
    ----------
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="person.xsl"?>
    <!DOCTYPE person_list SYSTEM "validator.dtd">

    <person_list>
        <person id="1" name="John Smith">
            <age>35</age>
            <height units="cm">173</height>
            <weight units="kg">75</weight>
        </person>
        <person id="2" name="Tom Stone">
            <age>26</age>
            <height units="cm">177</height>
            <weight units="kg">65</weight>
        </person>
        <person id="3" name="Mark Glennford">
            <age>17</age>
            <height units="cm">181</height>
            <weight units="kg">61</weight>
        </person>
    </person_list>

    ----------
    person.xsl
    ----------
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:my="http://www.mysite.com/my">

      <xsl:function name="my:age">
        <xsl:param name="age"/>

        <xsl:for-each select="person_list/person">
          <xsl:if test="age &gt; $age">
            <!-- code to display data: age, height, weight-->
          </xsl:if>
        </xsl:for-each>
      </xsl:function>

    <xsl:template match="/">
        <xsl:value-of select="my:age(18)"/><!-- isn't it supposed to output the data for person with ids "1" and "2"? -->
    </xsl:template>

    </xsl:stylesheet>

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 27, 2010 @ 01:50
    Chriztian Steinmeier
    0

    Hi,

    Unfortunately you can't use XSLT 2.0 features in Umbraco (though on the face of it, it doesn't look like you're trying to do this in Umbraco).

    .NET only supports XSLT 1.0, so if you're actually trying this in a macro, you'll have to find another solution. 

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 27, 2010 @ 02:23
    Chriztian Steinmeier
    0

    Having said that, I'll gie you this as a starting point (though these forums should be kept on-topic for Umbraco XSLT issues):

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:my="http://www.mysite.com/my"
    >
    
        <xsl:function name="my:check" as="xs:string">
            <xsl:param name="person" as="element()" />
            <xsl:param name="age" as="xs:integer" />
            <xsl:value-of select="concat($person/@name, if ($person/age &gt; $age) then ' is old enough.' else ' is too young.')" />
        </xsl:function>
    
        <xsl:template match="person">
            <p>
                <xsl:value-of select="my:check(., 18)" />
            </p>
        </xsl:template>
    </xsl:stylesheet>

     

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft