xsl-list
[Top] [All Lists]

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

2016-10-16 20:23:12
Finally:

  empty($vF-Ids[index-of($vF-Ids,.)[2]])

where the $vF-Ids sequence is:

  //*/name()[starts-with(., 'F')]

Here is a small XSLT 2.0 transformation to use to evaluate these expressions:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="text"/>

  <xsl:variable name="vFNames" select="//*/name()[starts-with(., 'F')]"/>

 <xsl:template match="/">
    <xsl:sequence select=    "empty($vFNames[index-of($vFNames,.)[2]])"/>
  </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<Root>

                <D1>

                                <D2/>

                                <F1/>

                </D1>

                <D2>

                                <F2/>

                                <F1/>

                </D2>

</Root>

the result is:

  false


When applied on this XML document (the second <F1/>  has been renamed to <F3>:

<Root>

                <D1>

                                <D2/>

                                <F1/>

                </D1>

                <D2>

                                <F2/>

                                <F3/>

                </D2>

</Root>

the result is:

  true


In XPath 3 one can use:

let $vFNames := //*/name()[starts-with(., 'F')]
  return
     empty($vFNames[index-of($vFNames,.)[2]])


or (if you like more the previous expression):

let  $vFNames := //*/name()[starts-with(., 'F')]
  return
      count($vFNames) eq count(distinct-values($vFNames))

Cheers,
Dimitre

On Sun, Oct 16, 2016 at 5:59 PM, Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com> wrote:
  count(//*[starts-with(name(), 'F')])
eq
  count(distinct-values(//*[starts-with(name(), 'F')]/name()))

Just a little bit shorter:

     count(//*[starts-with(name(), 'F')])
    eq
     count(distinct-values(//*/name()[starts-with(., 'F')]))


Cheers,
Dimitre

On Sun, Oct 16, 2016 at 5:56 PM, Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com> wrote:
  count(//*[starts-with(name(), 'F')])
eq
  count(distinct-values(//*[starts-with(name(), 'F')]/name()))


This is just a first try. There could be even a shorter expression.


Cheers,
Dimitre.

On Sun, Oct 16, 2016 at 4:29 PM, G. Ken Holman 
g(_dot_)ken(_dot_)holman(_at_)gmail(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:
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(_at_)mitre(_dot_)org 
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(_at_)CraneSoftwrights(_dot_)com |
Google+ blog _ _ _ _ _ http://plus.google.com/+GKenHolman-Crane/posts |
Legal business disclaimers: _ _ http://www.CraneSoftwrights.com/legal |





--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>