[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Seek an XPath 2.0 expression for checking that eac

Subject: Re: Seek an XPath 2.0 expression for checking that each object in a file system has one parent
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 16 Oct 2016 23:29:15 -0000
Re:  Seek an XPath 2.0 expression for checking that eac
I see that you are dealing with only the names of the grandchildren of the root. The vollowing has three solutions, one based on the names (as you have done) and two that are namespace-safe. The latter two are equivalent, but since the operators "every" and "some" didn't come to mind for you, I thought I would illustrate both.

The second two work with axes the way you were getting started. But for the first I think you can rephrase your problem to be not that all elements have one parent but that there is only one of every element ... which I think is equivalent given the limited amount of information regarding your objective.

I hope this helps.

. . . . . . Ken

~/t/ftemp $ cat roger1.xml
<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <D1>
        <D2/>
        <F1/>
    </D1>
    <D2>
        <F2/>
    </D2>
</Root>
~/t/ftemp $ cat roger2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <D1>
        <D2/>
        <F1/>
    </D1>
    <D2>
        <F2/>
        <F1/>
    </D2>
</Root>
~/t/ftemp $ xslt2 roger1.xml roger.xsl
true
true
true
~/t/ftemp $ xslt2 roger2.xml roger.xsl
false
false
false
~/t/ftemp $ cat roger.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:value-of select="
    count(/Root/*/*)=count(distinct-values(/Root/*/*/name(.)))"/>
  <xsl:text>&#xa;</xsl:text>
  <xsl:value-of select="
    every $elem in /Root/*/* satisfies
          not($elem/following::*[not(*)]/node-name(.)=node-name($elem))"/>
  <xsl:text>&#xa;</xsl:text>
  <xsl:value-of select="
    not( some $elem in /Root/*/* satisfies
              $elem/following::*[not(*)]/node-name(.)=node-name($elem))"/>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

</xsl:stylesheet>
~/t/ftemp $


At 2016-10-16 22:33 +0000, Costello, Roger L. costello@xxxxxxxxx wrote:
Hi Folks,

I am modeling a file system. Below is a sample instance. D1 means Directory 1, F1 means File 1, etc. The instance says this: the content of directory 1 is directory 2 and file 1. The content of directory 2 is file 2. Stated another way, directory 2 and file 1 are contained in directory 1, and file 2 is contained in directory 2.

<Root>
    <D1>
        <D2/>
        <F1/>
    </D1>
    <D2>
        <F2/>
    </D2>
</Root>

I want an XPath 2.0 expression which returns true if each object has one parent. An "object" is a directory or a file. In the example above each object has one parent, so the XPath should return true. Below is an illegal file system because F1 has two parents: D1 and D2.

<Root>
    <D1>
        <D2/>
        <F1/>
    </D1>
    <D2>
        <F2/>
        <F1/>
    </D2>
</Root>

The XPath should return false.

This XPath is almost correct:

for $i in /Root/* return for $j in $i/* return not(name($j) = $i/following-sibling::*/*/name())

I say it is "almost" correct because it returns multiple Booleans, not a single Boolean result.

Two Questions:

1. What is the correct XPath expression?
2. Is there a different way to model in XML a file system that would enable a simple XPath expression?


/Roger


--
Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Streaming hands-on XSLT/XPath 2 training @US$45: http://goo.gl/Dd9qBK |
Crane Softwrights Ltd. _ _ _ _ _ _ http://www.CraneSoftwrights.com/s/ |
G Ken Holman _ _ _ _ _ _ _ _ _ _ mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ blog _ _ _ _ _ http://plus.google.com/+GKenHolman-Crane/posts |
Legal business disclaimers: _ _ http://www.CraneSoftwrights.com/legal |

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.