Validate a sequence on elements with Xslt

Discussion in 'XML' started by Juho Jussila, Dec 1, 2005.

  1. Juho Jussila

    Juho Jussila Guest

    Hi

    How can I easily ensure that Xml document has elements in certain
    order? I think it can be done with Xml schema, but I'd like to use
    Xslt, because the validation is a part of Xslt transformations.

    Xml document:
    <Root>
    <Foo/>
    <Bar/>
    <Bar/>
    <Baz/>
    <Foo/> <!-- this is in the wrong position -->
    </Root>

    All elements don't necessary have to exist, but if they do they must
    be in the right position. I'd like to have similar a function like this
    awk script:

    $ echo "FooBarBarBazFoo" | awk '$0 !~ /^(Foo)*(Bar)*(Baz)*$/ { print "Error"}'


    This is my attempt, quite ugly code. Is there a better way to do this ?
    -----
    <xsl:template match="/Root">
    <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="Foo[not(preceding-sibling::*[name() != 'Foo'])]"/>

    <xsl:template match="Bar[not(preceding-sibling::*[name()!='Foo' and
    name()!='Bar'])]"/>

    <xsl:template match="Baz[not(preceding-sibling::*[name()!='Foo' and
    name()!='Bar' and
    name()!='Baz'])]"/>
    <xsl:template match="*">
    <xsl:message terminate="yes">
    <xsl:text>Error in: </xsl:text>
    <xsl:value-of select="name()"/>
    </xsl:message>
    </xsl:template>
    -----

    Thanks
    --
    //
    \X/
    Juho Jussila, Dec 1, 2005
    #1
    1. Advertising

  2. I would be using XML Schema and you can make it work with your XSLT
    using either XSLT output validation and/or a Schema Aware XSLT
    processor like Saxon. Not sure if it makes sense to write your own
    validation scripts. Hope that helps.

    Sincerely,
    The Stylus Studio Team
    http://www.stylusstudio.com
    Stylus Studio, Dec 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    649
    Crutcher Dunnavant
    Feb 26, 2006
  2. Gerald Aichholzer
    Replies:
    2
    Views:
    2,474
    Gerald Aichholzer
    Jun 27, 2006
  3. Giacomo
    Replies:
    7
    Views:
    1,618
    Dimitre Novatchev
    Aug 13, 2006
  4. stef mientki
    Replies:
    13
    Views:
    608
    stef mientki
    Oct 20, 2007
  5. RolfK
    Replies:
    2
    Views:
    571
    RolfK
    Oct 9, 2008
Loading...

Share This Page