2 Replies Latest reply on Nov 1, 2016 8:06 AM by Loic Aigon

    Extendscript xml with dash element node

    sym47051608

      Hi all,

       

      How do you access to an xml element with dash inside it? And modify it?

      Exemple:

       

      <root>

           <bookstore>

                <Author>

                     <book-en>sample</book-en>

                     <book-fr>test</book-fr>

                     <book-de>testtest</book-de>

                     etc.....

       

      I try with

      myxml.bookstore.Author.book-en="toto"-->Not work

      myxml.bookstore.Author.('book-en')="toto"-->Not work

      myxml.bookstore.Author.child(1)="toto"-->Not work

       

      Please, can you help me??? Thx a lot!! ;-)

       

      Syme

        • 1. Re: Extendscript xml with dash element node
          Dirk Becker Level 4
          var x = <xml><test-ing>Hello</test-ing>, <ot-her>World!</ot-her></xml>;
          alert(x["test-ing"]);
          x["test-ing"] = "Good bye";
          alert(x.toXMLString());
          
          • 2. Re: Extendscript xml with dash element node
            Loic Aigon Level 2

            Dirk's answer is obviously one the smartest. Let me help you understanding where you were wrong.

             

            myxml.bookstore.Author.book-en

            fails because of the dash obviously

             

            myxml.bookstore.Author.('book-en')

            Fails because you, by using parens, indicates a condition which is here kind of undefined : 'book-en' so what…

             

            myxml.bookstore.Author.child(1)

            Fails because you are misusing the child function as it expects a string and not an index.

             

            So you could have used

             

            myxml.bookstore.Author.children()[0] = "titi";

            The less precise because you can't presume the first child is kind of book-en.

            Similar to /root/bookstore/Author/*[1] in xpath

             

            myxml.bookstore.Author.child ( "book-en" )[0] = "tata"

            You are here targeting the first child of type "book-en"

            Similar to /root/bookstore/Author/book-en[1] in xpath

             

             

            myxml.bookstore.Author.children().(name()=='book-en')[0]="toto";

            You are here using a condition to retrieve only nodes of the children list whose name matches 'book-en'

            Similar to /root/bookstore/Author[name(.)='book-en'][1] in xpath

             

            myxml.bookstore.Author.descendants("book-en")[0] = "tutu";

            This is the most global approach as it would look at any descendants from the starting node.

            Similar to /root/bookstore/Author//book-en[1] in xpath

             

            But once again, Dirk's answer is the smartest one you want to use.