xPath question

C

cgaden

Hi everyone,

I have a problem that I've been trying to figure out for a while but I
just can't seem to get it right. I have the following XML fragment:

<Page ID="x1" Schema="Folder">
-<Page ID="x10" Schema="Common">
--<Page ID="x100" Schema="Common" />
--<Page ID="x200" Schema="Common" />
--<Page ID="x300" Schema="Common" />
---<Page ID="x301" Schema="Folder">
----<Page ID="x3010" Schema="Link" />
----<Page ID="x3011" Schema="Link" />
----<Page ID="x3012" Schema="Link" />
----<Page ID="x3013" Schema="Link" />
----<Page ID="x3014" Schema="Link" />
----<Page ID="x3015" Schema="Link" />
----<Page ID="x3016" Schema="Link" />
---</Page>
---<Page ID="x302" Schema="Common">
----<Page ID="x3020" Schema="Common"/>
---</Page>
---<Page ID="x303" Schema="Common">
----<Page ID="x3030" Schema="Link" />
----<Page ID="x3031" Schema="Link" />
---</Page>
---<Page ID="x304" Schema="List">
----<Page ID="x3040" Schema="Folder" />
---</Page>
---<Page ID="x305" Schema="Common" />
--<Page ID="x400" Schema="Common" />
--<Page ID="x500" Schema="Common" />
--<Page ID="x600" Schema="Common" />
-</Page>
</Page>

I'm trying to retrieve all of the Pages below x300 (not including x300)
except those where the Schema value is 'Folder' or the parent page's
Schema value is 'Folder' (specifically these pages should be excluded:
ID = x301 [and children x3010 - x3016] and x3040). I've been able to
successfully remove x301 and children using this xPath query:

//*[@ID='x300']/Page[@Schema != 'Folder']

However, when I modify the query to:

//*[@ID='x300']/descendant-or-self::page[@Schema != 'Folder']

The resultset breaks completely with all of the 3 nodes getting
promoted to level 2.

I've also tried to use the ancestor-or-self method but this also does
not return the results I'm attempting to retrieve - I actually get no
results. I believe that this occurs because there is a Page with a
Schema value of 'Folder' below my initial criteria and that the
sub-selection occurs after the attribute restriction is applied.

I appreciate any help. Thank you.

-Christian
 
S

Soren Kuula

cgaden said:
Hi everyone,
I'm trying to retrieve all of the Pages below x300 (not including x300)

Below, is that:

- a smaller number than

- a descendant of

- apperaring later in the docuemnt than

???
except those where the Schema value is 'Folder' or the parent page's
Schema value is 'Folder' (specifically these pages should be excluded:
ID = x301 [and children x3010 - x3016] and x3040). I've been able to
successfully remove x301 and children using this xPath query:

//*[@ID='x300']/Page[@Schema != 'Folder']
The resultset breaks completely with all of the 3 nodes getting
promoted to level 2.

Level ;) ? XPath node sets are flat (I presume you know) ;)
I've also tried to use the ancestor-or-self method but this also does
not return the results I'm attempting to retrieve - I actually get no
results. I believe that this occurs because there is a Page with a
Schema value of 'Folder' below my initial criteria and that the
sub-selection occurs after the attribute restriction is applied.

I appreciate any help. Thank you.

-Christian
The select exp in this stylesheet is my bet (the rest of it is just a
demonstrator application, don't worry if you don't know XSLT):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://dongfang.dk/testdata"
xmlns:input="http://dongfang.dk/testdata"
version="1.0">

<xsl:template match="/">
<xsl:apply-templates
select="descendant::page[@ID='x300']/following::page[not(@Schema =
'Folder' or parent::*/@Schema = 'Folder')]"/>
</xsl:template>

<xsl:template match="Page">
Got page w ID: <xsl:value-of select="@ID"/>
</xsl:template>

<xsl:template match="*">
Whoops, the expression selected something that was not a page. Back
to the drawing board you go.
</xsl:template>

</xsl:stylesheet>


Spews out
<?xml version="1.0"?>

Got page w ID: x302
Got page w ID: x3020
Got page w ID: x303
Got page w ID: x3030
Got page w ID: x3031
Got page w ID: x304
Got page w ID: x305
Got page w ID: x400
Got page w ID: x500
Got page w ID: x600


when applied 2 yr doc.

Soren
 
C

cgaden

Hi Soren,

Unfortunately it did not work as I had hoped. In normal circumstances I
would remove the nodes using my XSLT stylesheet (in fact that's one of
the tasks my stylesheet does now) but I'm working within the framework
of a black-box application and in order to improve that application's
performance I'm attempting to remove the excess nodes prior to
processing the data with the stylesheet.

-Christian
 
C

cgaden

Hi Everyone,

Thank you all for the assistance. I just realized that my sample XML
from my original post actually contains an error. I apologize to the
group for my mistake.

The error was that node ID = x300 was supposed to be the
parent/grandparent for all other nodes IDed x30[n[n]].

The XML that I'm trying to parse is actually the following:

<Page ID="x1" Schema="Folder">
-<Page ID="x10" Schema="Common">
--<Page ID="x100" Schema="Common" />
--<Page ID="x200" Schema="Common" />
--<Page ID="x300" Schema="Common">
---<Page ID="x301" Schema="Folder">
----<Page ID="x3010" Schema="Link" />
----<Page ID="x3011" Schema="Link" />
----<Page ID="x3012" Schema="Link" />
----<Page ID="x3013" Schema="Link" />
----<Page ID="x3014" Schema="Link" />
----<Page ID="x3015" Schema="Link" />
----<Page ID="x3016" Schema="Link" />
---</Page>
---<Page ID="x302" Schema="Common">
----<Page ID="x3020" Schema="Common"/>
---</Page>
---<Page ID="x303" Schema="Common">
----<Page ID="x3030" Schema="Link" />
----<Page ID="x3031" Schema="Link" />
---</Page>
---<Page ID="x304" Schema="List">
----<Page ID="x3040" Schema="Folder" />
---</Page>
---<Page ID="x305" Schema="Common" />
--</Page>
--<Page ID="x400" Schema="Common" />
--<Page ID="x500" Schema="Common" />
--<Page ID="x600" Schema="Common" />
-</Page>
</Page>

The exact result I'm trying to retrieve is this (all of the
child/grandchild nodes of x300 that are not Schema = 'Folder' or
children of pages where Schema = 'Folder'):

<Page ID="x302" Schema="Common">
-<Page ID="x3020" Schema="Common"/>
</Page>
<Page ID="x303" Schema="Common">
-<Page ID="x3030" Schema="Link" />
-<Page ID="x3031" Schema="Link" />
</Page>
<Page ID="x304" Schema="List" />
<Page ID="x305" Schema="Common" />

Again, thank you all for your input.

-Christian
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top