Validate a sequence on elements with Xslt

J

Juho Jussila

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>
 
S

Stylus Studio

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top