data.zip

Sub Entries with the same Name can't be selected

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    Leiningen 2.7.0 on Java 1.8.0_91 Java HotSpot(TM) 64-Bit Server VM

Description

I want to select the content of an XML element named "Group" which itself is in an element named "Group" using xml-zip/xml1. Instead of returning the content of the inner "Group" the outer "Group" element matches. The approach of how to select this does not work and I suspect this might be a defect.

Please see the minimal example:

XML:

root:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Group>
    <Name>Outer</Name>
    <Group>
      <Name>Inner</Name>
    </Group>
  </Group>
</Root>

(zip-xml/xml1-> root :Root :Group :Group :Name zip-xml/text)
"Outer"

Leiningen project with unit-tests as attachment. Run: lein test

$ lein test
lein test :only zip-xml-bug.core-test/parsing-group-elements

FAIL in (parsing-group-elements) (core_test.clj:34)
selecting the name of inner
expected: (= "Inner" (zip-xml/xml1-> root :Root :Group :Group :Name zip-xml/text))
  actual: (not (= "Inner" "Outer"))

Ran 1 tests containing 2 assertions.
1 failures, 0 errors.
Tests failed.
  1. zip-xml-bug.tgz
    19/Sep/16 3:04 PM
    6 kB
    Benjamin Peter
  2. zip-xml-bug-descent.tgz
    20/Sep/16 2:41 PM
    7 kB
    Benjamin Peter

Activity

Hide
Benjamin Peter added a comment -

I found a "workaround" as you can see below and in zip-xml-bug-descent.tgz

(defn descent=
  [tagname]
  (fn [loc]
        (filter #(and (zip/branch? %) (= tagname (:tag (zip/node %))))

  (testing "selecting the name of inner using descent"
    (is (= "Inner"
           (-> (zip-xml/xml1-> root :Root :Group (descent= :Group) :Name zip-xml/text)))))

It seems the first expression in tag= matching the element itself in the or expression is the problem in my case. I suspect it can be used to select the root element. Is there any other need for it?

(defn tag=
  [tagname]
  (fn [loc]
    (or (= tagname (:tag (zip/node loc)))
        (filter #(and (zip/branch? %) (= tagname (:tag (zip/node %))))
(zf/children-auto loc)))))

Maybe there should be a self predicate instead?

Show
Benjamin Peter added a comment - I found a "workaround" as you can see below and in zip-xml-bug-descent.tgz
(defn descent=
  [tagname]
  (fn [loc]
        (filter #(and (zip/branch? %) (= tagname (:tag (zip/node %))))

  (testing "selecting the name of inner using descent"
    (is (= "Inner"
           (-> (zip-xml/xml1-> root :Root :Group (descent= :Group) :Name zip-xml/text)))))
It seems the first expression in tag= matching the element itself in the or expression is the problem in my case. I suspect it can be used to select the root element. Is there any other need for it?
(defn tag=
  [tagname]
  (fn [loc]
    (or (= tagname (:tag (zip/node loc)))
        (filter #(and (zip/branch? %) (= tagname (:tag (zip/node %))))
(zf/children-auto loc)))))
Maybe there should be a self predicate instead?
Hide
Denis Shilov added a comment -

It is regression from 0.1.1 to 0.1.2

Commit that changed behaviour is https://github.com/clojure/data.zip/commit/c5d6ca25c128f9fe937b11505c7c9736cfa2dd9a

Simple test to check

This works in 0.1.1

(def nestedxml
  (parse-str "<doc><area><area>1</area><unit>033</unit></area></doc>"))

(deftest same-nested-tags
  (is (= "1" (xml1-> nestedxml :area :area text)))
  (is (= "033" (xml1-> nestedxml :area :unit text))))

Related bug is DZIP-3

Show
Denis Shilov added a comment - It is regression from 0.1.1 to 0.1.2 Commit that changed behaviour is https://github.com/clojure/data.zip/commit/c5d6ca25c128f9fe937b11505c7c9736cfa2dd9a Simple test to check This works in 0.1.1
(def nestedxml
  (parse-str "<doc><area><area>1</area><unit>033</unit></area></doc>"))

(deftest same-nested-tags
  (is (= "1" (xml1-> nestedxml :area :area text)))
  (is (= "033" (xml1-> nestedxml :area :unit text))))
Related bug is DZIP-3
Hide
Bastien Guerry added a comment -

For what is worth, I've just been hit by this regression too.
I hope a proper fix can be released soon! Thanks in advance.

Show
Bastien Guerry added a comment - For what is worth, I've just been hit by this regression too. I hope a proper fix can be released soon! Thanks in advance.
Hide
Benjamin Peter added a comment - - edited

My example does not work with 0.1.1 either, I doubt it is just a regression. @Denis Shilov maybe you want to create another ticket for this.

Show
Benjamin Peter added a comment - - edited My example does not work with 0.1.1 either, I doubt it is just a regression. @Denis Shilov maybe you want to create another ticket for this.
Hide
Paul Dlug added a comment -

Any update on this? We're also hitting this bug.

Show
Paul Dlug added a comment - Any update on this? We're also hitting this bug.
Hide
Paul Dlug added a comment -

This worked for us with 0.2.0-alpha2 when copying the latest implementation of tag= without the or part as Benjamin Peter suggested. I'm not sure what the best fix is here seems tricky to accommodate the previous patch to allow it to match the root, clearly matching descendants it is more frequent case so perhaps a root= predicate is preferred over introducing something like descendant= through all the xml-> matchers. Of course if there's some fix to tag= which can support both that I'm not seeing that would be best but seems tricky.

Show
Paul Dlug added a comment - This worked for us with 0.2.0-alpha2 when copying the latest implementation of tag= without the or part as Benjamin Peter suggested. I'm not sure what the best fix is here seems tricky to accommodate the previous patch to allow it to match the root, clearly matching descendants it is more frequent case so perhaps a root= predicate is preferred over introducing something like descendant= through all the xml-> matchers. Of course if there's some fix to tag= which can support both that I'm not seeing that would be best but seems tricky.
Hide
Brian Stearns added a comment - - edited

Ran into the same thing just today. Posted this (https://stackoverflow.com/questions/46535423/cant-access-deeply-nested-xml-with-clojure-data-zip-xml) a bit ago, but now that I've found this I know I'm not alone/crazy.

@bpeter thanks for the workaround. Does anyone know if fixing this is making it into 0.1.2/is there anything I can do to help make that happen?

Show
Brian Stearns added a comment - - edited Ran into the same thing just today. Posted this (https://stackoverflow.com/questions/46535423/cant-access-deeply-nested-xml-with-clojure-data-zip-xml) a bit ago, but now that I've found this I know I'm not alone/crazy. @bpeter thanks for the workaround. Does anyone know if fixing this is making it into 0.1.2/is there anything I can do to help make that happen?
Hide
Alex Miller added a comment -

Someone needs to dig in to see if there is a solution that lets people do what they want in DZIP-3 and here in DZIP-6. Does the patch here re-break the case in DZIP-3?

If so, then more work needs to be done to either find a solution that works for both or to decide whether one of these cases is not valid and shouldn't be supported, or to add something that lets you do both.

Bumping up a notch, I'd love to have someone signup to be an active maintainer for data.zip. I've helped out here on a drive-by approach, but I have no skin in this game. Given there are a bunch of (obviously) caring users here, it would be great to have help from one of you.

Show
Alex Miller added a comment - Someone needs to dig in to see if there is a solution that lets people do what they want in DZIP-3 and here in DZIP-6. Does the patch here re-break the case in DZIP-3? If so, then more work needs to be done to either find a solution that works for both or to decide whether one of these cases is not valid and shouldn't be supported, or to add something that lets you do both. Bumping up a notch, I'd love to have someone signup to be an active maintainer for data.zip. I've helped out here on a drive-by approach, but I have no skin in this game. Given there are a bunch of (obviously) caring users here, it would be great to have help from one of you.

People

Vote (4)
Watch (3)

Dates

  • Created:
    Updated: