Git Product home page Git Product logo

Comments (22)

biswanaths avatar biswanaths commented on July 17, 2024

Thanks Graeme for taking time to re-reporting this issue from scala JIRA. We are in the process of migrating all the open issues from JIRA to github. I have left my test work on the migration as comment so that community can review it. ( #62)

Thanks again.

from scala-xml.

nikhilsinha avatar nikhilsinha commented on July 17, 2024

If no one's picked this up yet, I'd be happy to take a wack at it.

from scala-xml.

biswanaths avatar biswanaths commented on July 17, 2024

Glad that you would like to help. We will be more than happy if we can get some help from you. Please let me know if you need any help from me.

Good luck.

from scala-xml.

mbeckerle avatar mbeckerle commented on July 17, 2024

I wanted to comment on this issue.

Technically, order of attributes is not significant in XML. A proper equality comparison of two XML objects would find them equal even if the attributes are in different orders.

However, I understand the motivation for wanting to reduce deltas. The library shouldn't change things that don't need to be changed.

A related issue is changes of namespace prefixes for attributes or elements. The actual prefixes used are part of the XML Infoset, so techically <a:b xmlns:a="urn:foo"/> is not always equal to <c:b xmlns:c="urn:foo"/> because their infoset contents are different. XML is allowed to be sensitive to the specific characters used in the prefixes of the QNames.

This suggests that there are really several different levels of equality and correspondingly preservation (which is what this issue is about).

  • preserve order of attributes (and do comparison/equality where order of attributes matters)
  • preserve XML infoset (and equality where everything about XML infoset is compared, and only that - so attribute order doesn't matter, but specific prefix characters do matter)
  • preserve XML namespace-aware infoset (and equality where namespace prefixes are interpreted as their namespaces and the specific characters of the prefix are disregarded, and attribute order doesn't matter)

from scala-xml.

ashawley avatar ashawley commented on July 17, 2024

@biswanaths Do I need to make a pull request out of my branch, or can the commit (only the 2nd one, sorry) get pulled in here as a first-class citizen by you (as the admin)?

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

any updates on this pull request?

from scala-xml.

SethTisue avatar SethTisue commented on July 17, 2024

What if you use ConstructingParser, is the order still scrambled?

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

hey @SethTisue
I guess I am not able to load my document using ConsructingParser

My file

 $cat /tmp/person.xml
 <root xmlns="http://www.nyorg.com/metadata/config" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd">
    <!-- This is important comment about person-->
    <person>
      <firstName>John</firstName>
      <lastName>Doe</lastName>
      <email>[email protected]</email>
      <city>SomeWhere</city>
      <property name="myName" value="someValue" />
    </person>
  </root>

This is how I am using it

val source = io.Source.fromURL(new java.io.File("/tmp/person.xml").toURL)
val d = scala.xml.parsing.ConstructingParser.fromSource(source, preserveWS = true)
val doc = d.document()
println(doc)

What I get back?

:1:1: < expected<root xmlns="http://www.nyorg.com/metadata/config" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd">^
doc: scala.xml.Document = null
null

On this other hand when I load the same file using XML.loadFile as

val doc = XML.loadFile("/tmp/person.xml")
println(doc)

It seems to load it correctly

<root xsi:noNamespaceSchemaLocation="../xschema/AppConfig.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:core="http://www.myorg.com/metadata/commonschematypes" xmlns="http://www.nyorg.com/metadata/config">
    
    <person>
      <firstName>John</firstName>
      <lastName>Doe</lastName>
      <email>[email protected]</email>
      <city>SomeWhere</city>
      <property value="someValue" name="myName"/>
    </person>
  </root>

but semantically has different orders and removed comments as documented http://stackoverflow.com/questions/40941628/scala-loadfile-xml-reorders-original-xml-and-removes-comments

any help resolving this is appreciated

from scala-xml.

SethTisue avatar SethTisue commented on July 17, 2024

@hhimanshu I'm sorry but this is an issue tracker, not a support forum. if you have a question about ConstructingParser, ask on Stack Overflow. if you become convinced there is a bug in ConstructingParser, you can open a new issue here.

from scala-xml.

ashawley avatar ashawley commented on July 17, 2024

Looks like Constructing Parser has the same issue. I'll add tests to the previous tests and make them in to a PR.

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

I added the question on Stackoverflow. http://stackoverflow.com/questions/40942075/constructingparser-could-not-load-valid-xml-document-from-file

Thanks

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

I was able to use ConstructingParser, but I can prove that even ConstructingParser reorders attributes. For details, see http://stackoverflow.com/a/40942534/379235

from scala-xml.

SethTisue avatar SethTisue commented on July 17, 2024

good to know, thanks @hhimanshu

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

Shall I open a Scala Issue for that?

from scala-xml.

SethTisue avatar SethTisue commented on July 17, 2024

Shall I open a Scala Issue for that?

for what?

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

ConstructingParser not preserving the order of element attributes

from scala-xml.

SethTisue avatar SethTisue commented on July 17, 2024

oh, I don't think we need a separate ticket for that, it's fine just as comments here on this one.

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

ok Thanks

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

I am thinking if there is anything I could do at the moment to unblock myself

from scala-xml.

hhimanshu avatar hhimanshu commented on July 17, 2024

The other way (not the best way, but probably acceptable for my use case) is following

  • Load the XML (using ConstructingParser)
  • Perform Manipulation(s)
  • f1 = Persist XML (intermediate representation with reversed attributes)
  • Load the XML (using ConstructingParser)
  • f1 = persist XML (final representation)

This is what I did

import scala.xml._
import java.io.File
import scala.xml.parsing.ConstructingParser



val path = "/tmp/"
val filePath = path + "person.xml"
val newFilePath = path + "person.intermediate.xml"
val finalFilePath = path + "person.final.xml"
val source = io.Source.fromURL(new File(filePath).toURL)

val cp = ConstructingParser.fromSource(source, true)
XML.save(newFilePath, cp.document().head)

val source1 = io.Source.fromURL(new File(newFilePath).toURL)
val cp1 = ConstructingParser.fromSource(source1, true)
XML.save(finalFilePath, cp1.document().head)

and on command-line, I see

$ /tmp cat person.xml; echo "----------"; cat person.final.xml
<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <!-- This is important comment -->
    <person>
        <fName>John</fName>
        <lName>Doe</lName>
        <property name="someName" value="someValue">p1</property>
    </person>
</root>
----------
<root>
    <!-- This is important comment -->
    <person>
        <fName>John</fName>
        <lName>Doe</lName>
        <property name="someName" value="someValue">p1</property>
    </person>
</root>

from scala-xml.

ashawley avatar ashawley commented on July 17, 2024

I was able to use ConstructingParser, but I can prove that even ConstructingParser reorders attributes.

The proposed fix in #172 also fixes ConstructingParser as well.

from scala-xml.

ashawley avatar ashawley commented on July 17, 2024

New comment to scala-user on Aug-14-2017:

http://www.scala-archive.org/Scala-XML-and-attribute-reordering-td3083550.html

from scala-xml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.