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