Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Validate a sequence on elements with Xslt

From: Juho Jussila <juho.jussila@---.-->
To: NULL
Date: 12/1/2005 4:03:00 PM
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
-- 
  //  juho.jussila@i...
\X/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent