xslt1 problem (sets?)

Tim (11)

Usenet Archive
Joined
Jan 7, 2011
Messages
21
Reaction score
0
hi, I have some documents that need to be processed in one way if the section titles belong to a certain set of titles and another way if a section title is not in that set. I thought I had a pretty clever solution but alas it doesn't work :) I'm using xsltproc on freebsd.

This isn't working, but it is what I've tried.
(1) get a node set created from splitting a given string of the canonical section titles and call that $need. (say the titles are 'Red', 'Green' and 'Blue', just for testing).

(2) get a node set of section titles from a document and call that $have. the titles match for a test case, 'Red', 'Green', and 'Blue'.

(3) calculate the intersection of $have and $need. If the number of nodes in $have is greater than the number of nodes in the intersection, we have a title that is not in the first list ($need).

However, my intersection is empty. Maybe someone can help me figure out how to solve this problem, using any method at all (intersecting sets or not).

thanks,
--Tim Arnold

<xsl:template match="/">
<xsl:variable name="need">
<xsl:copy-of select="exsl:node-
set(str:split('Green,Blue,Red',','))" />
</xsl:variable>

<xsl:variable name="have">
<xsl:for-each select="chapter//section/info/title">
<xsl:element name="token">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:variable>

<xsl:copy-of select="exsl:node-set(set:intersection($have, $need))" /> </xsl:template> </xsl:stylesheet>
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
At a very quick glance, and assuming that set: and str: are also referring to the EXSLT extensions (which are not necessarily supported everywhere since they *are* extensions):

1) set:intersection appears to be defined as operating on node identity, not string value.

2) By generating the xsl:variable value via the children of that element, rather than via a select= attribute, you are creating a new Temporary Tree (or, if you prefer the XSLT 1.0 terminology, a new Result Tree Fragment). That's a new set of nodes, which will not have the same node identity as nodes in the original document.

The result is that you're comparing a set of new text nodes containing your strings, against a set of new nodes copied from the source document... for identity. That isn't going to ever match.
 

Tim (11)

Usenet Archive
Joined
Jan 7, 2011
Messages
21
Reaction score
0
At a very quick glance, and assuming that set: and str: are also referring to the EXSLT extensions (which are not necessarily supported everywhere since they *are* extensions):

1) set:intersection appears to be defined as operating on node identity, not string value.

2) By generating the xsl:variable value via the children of that element, rather than via a select= attribute, you are creating a new Temporary Tree (or, if you prefer the XSLT 1.0 terminology, a new Result Tree Fragment). That's a new set of nodes, which will not have the same node identity as nodes in the original document.

The result is that you're comparing a set of new text nodes containing your strings, against a set of new nodes copied from the source document... for identity. That isn't going to ever match.

Hi Joe,
Thanks for the explanation. I wondered if I was comparing for identity but lacked the knowledge to see it.

So now I know that isn't going to work. Can you point me in the right direction for solving the problem? I'm obviously pretty new to xslt and get the basics I think, but this situation is a real puzzle for me. Not asking for a working solution, just a direction that might be profitable to study. I used the 'set' functionality since that's how I would solve the problem in a procedural language.

thanks for thinking about this,
--Tim Arnold
 

Tim (11)

Usenet Archive
Joined
Jan 7, 2011
Messages
21
Reaction score
0
Hi Joe,
Thanks for the explanation. I wondered if I was comparing for identity but lacked the knowledge to see it.

So now I know that isn't going to work. Can you point me in the right direction for solving the problem? I'm obviously pretty new to xslt and get the basics I think, but this situation is a real puzzle for me. Not asking for a working solution, just a direction that might be profitable to study. I used the 'set' functionality since that's how I would solve the problem in a procedural language.

thanks for thinking about this,
--Tim Arnold

Well I do have something working but I think it could be better. I create $need as a string of concatenated permissible titles. Then I look at each first-level section title and see if $need contains the title, putting the result (true or false) into a new variable $mydecision.

After that loop, if 'false' is present in the $mydecision variable, the decision is then false.

In this toy example that isn't too bad, but if someone had a title named "Gree" my test would erroneously pass.

I'm still working on this problem--if anyone sees a more fruitful method to think about please let me know. thanks,
--Tim

<xsl:template match="/">
<xsl:variable name="need">
<xsl:value-of select="string('Red,Green,Blue)" />
</xsl:variable>

<xsl:variable name="mydecision">
<xsl:for-each select="d:chapter/d:section/d:info/d:title"> <xsl:value-of select="contains($need, text)" /> </xsl:for-each> </xsl:variable>

<xsl:choose>
<xsl:when test="contains($mydecision,'false')">
<xsl:text>FALSE</xsl:text>
</xsl:when>
<xsl:eek:therwise>
<xsl:text>TRUE</xsl:text>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
 

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
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top