Re: [xsl] Seek an XPath 2.0 expression for checking that each object in a file system has one parent

Subject: Re: [xsl] 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
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