DonBaka
DonBaka

Reputation: 455

Do xsl/xslt 1.0 have features to check some straightforward conditions in xml files?

I'm using XSLT 1.0 to check some conditions against my xml files. The conditions are something like that:

I can see that we can always approach these conditions from the brute force method as we can extract any value and basic arithmetic is there to build up the conditions. But I'm interested to find out if there are keywords/functions supported in XSLT 1.0 to simplify the checks?

Upvotes: 0

Views: 384

Answers (2)

Michael Kay
Michael Kay

Reputation: 163595

Your three examples can all be done in XSLT 1.0, but XML Schema is probably more appropriate for this job.

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117140

To answer your first question:

• value of a particular-path element has unique values

consider the following example:

XML

<root>
    <parent>
        <child>Alpha</child>
        <child>Bravo</child>
        <child>Charlie</child>
    </parent>
    <parent>
        <child>Delta</child>
        <child>Alpha</child>
        <child>Echo</child>
    </parent>
</root>

To test if all values of /root/parent/child are unique, you can do:

in pure XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="child" match="child" use="."/>

<xsl:template match="/root">
    <result>
        <xsl:value-of select="not(parent/child[count(key('child', .)) > 1])" />
    </result>
</xsl:template>

</xsl:stylesheet>

with support for the EXSLT set:distinct() extension function

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:set="http://exslt.org/sets"
extension-element-prefixes="set">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/root">
    <result>
        <xsl:value-of select="count(parent/child)=count(set:distinct(parent/child))" />
    </result>
</xsl:template>

</xsl:stylesheet>

Please post your other questions separately.

Upvotes: 1

Related Questions