Re: [xsl] More XPath 1.0 Expression help

Subject: Re: [xsl] More XPath 1.0 Expression help
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 19 Jul 2006 18:22:51 -0400
Todd,

This is really OT, as although it's XPath it's not XSLT. Many of our assumptions and guesses would be wrong (as we've already found), which isn't fair.

I suppose given your constraints this pattern won't work:

<sch:rule context="(/plist/dict/key)[1]">

to avoid getting multiple reports.

So maybe you need to build in a test for "firstness" into your assertion (I'm assuming your data set is small):

sch:assert test="not(.=preceding::key) and ..."

Thus: test="not(.=preceding::key) and (.=following::key)"

... and report count(.|following::key) as your count of duplicates.

As to guiding you as to whether you're wrong about which constructs you can use in your particular Schematron setup (people are using XPath 2.0 in other Schematron setups btw), someone might be able to say, but they'd be speaking off topic too.

Oh well,
Wendell

At 05:52 PM 7/19/2006, you wrote:
Thanks Mike and Spencer, that definitely got me going in the right
direction!

However, I don't think I have XSLT grouping available to me in this
case... let me explain.

This XPath is to be used in a Schematron <sch:asstert> test
attribute. I am using the XSLT ref impl of Schematron 1.5 running
against libxslt witch is limited to XSLT 1.0 and XPath 1.0. I don't
think i can access exslt from the schematron schema ,so exslt is out.

Also, I'm assuming that I cannot access XSLT grouping mechanisms from
within the schematron schema, so that, unfortunately, is out too. : ( Please correct me if I'm wrong there.


So here's what i've got so far:



<sch:pattern name="Report Duplicate Keys">
<sch:rule context="/plist/dict/key">
<sch:assert test="count(../key[.=current()]) = 1">
Duplicate '<sch:value-of select="."/>' key exists
<sch:value-of select="count(../key[.=current()])"/> times.
Keys should be unique.</sch:assert>
</sch:rule>
</sch:pattern>



So I have access to the XSLT current() func but no XSLT grouping features AFAIK. This rule gets the job of detecting duplicate keys done... unfortunately it also raises n errors for n duplicate keys rather than just one error for n duplicate keys. That's certainly not a show-stopper... but it would be nice to fix.

Any ideas?

The O(n^2) complexity doesn't bother me in this instance as perf is
not a prime concern here and the data set is very small anyway.

Here's what the actual input looks like... it's an Apple plist:

<plist>
<dict>
<key>Key1</key>
<value/>
<key>Key2</key>
<value/>
<key>Key3</key>
<value/>
<key>Key1</key>
<value/>
</dict>
</plist>


Todd



On Jul 19, 2006, at 2:22 PM, Michael Kay wrote:


Take a look at http://www.jenitennison.com/xslt/grouping

Most of the techniques for grouping are at the XSLT level, but you
can use
the basic

key[not(. = preceding-sibling::key)]

if you don't mind O(n^2) complexity.

This detects the distinct values: leave out the not() and you have an
expression that detects the duplicates.

Michael Kay
http://www.saxonica.com/


-----Original Message----- From: Todd Ditchendorf [mailto:itod@xxxxxxxxx] Sent: 19 July 2006 22:18 To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: [xsl] More XPath 1.0 Expression help


I need some help constructing an XPath to detect nodes containing duplicate string-values. I am currently restricted to XPath 1.0 syntax , so no access to distinct-values() or exslt unfortunately.

I have a document like this:

<dict>
<key>Key1</key>
<value/>
<key>Key2</key>
<value/>
<key>Key3</key>
<value/>
<key>Key1</key>
<value/>
</dict>

How do I create an XPath 1.0 expression to detect the <key>
elements with duplicate string-values?


Thanks!



Todd Ditchendorf Software Engineer itod@xxxxxxxxx

Current Thread