4 Replies Latest reply on Apr 20, 2009 6:04 AM by norton35

    Need to create XML attribute


      I have a project which is nearing completion, but I've run into a problem. My ID document is a company directory. At the moment I am testing it with details for 50 companies, but the final output could be as many as 1,000. I've imported the data from a FileMaker database as XML. So far so good. Each 'Company' tag has an attribute containing the primary key of the company's details in the database. I would like to automatically find which page each company's details appear on, then write that page number as an another attribute in the 'Company' tag.


      The reason I want to do this is so that I can export the updated XML back to FileMaker and update the table with the Page Number the company appears on. This step is quite important but I really don't want to edit attributes by hand! I'm sure this must be fairly easy for a Javascript guru, but I'm strictly a PHP/MySQL person and I'm a bit lost!


      I would really appreciate it if someone could help me out with this!



        • 1. Re: Need to create XML attribute
          Dave Saunders Level 4

          I'd tackle this using JavaScript by walking through the xml structure, seeking the elements of interest. For each one, I'd use the texts[0] property to locate the page on which it resides, using the function discussed here:




          And then I'd create the attribute for the element using code along these lines:


          var attr = el.xmlAttributes.item("id");
          if (attr == null) {
             el.xmlAttributes.add("id", attrValue);
          } else {
            attr.value = attrValue;



          1 person found this helpful
          • 2. Re: Need to create XML attribute
            Olav Martin Kvern Level 3

            Hi Ian,


            Dave's answer is a good one (thanks, Dave!), but don't waste time trying to traverse the XML structure through iteration. Instead, you can use an XPath statement to find the XML elements you want, then use Dave's suggestion to find the location of the text on each page. Ideally, you'd create an XML rule to do this, but whether you do that or not depends upon how frequently you think you'll need it.


            Let's say you want to find all of the XML elements tagged with the XML tag "myElement". Here's what you'd do:


            //Given a document "myDocument"

            var myXMLElements = myDocument.xmlElements.item(0).evaluateXPathExpression("//myElement");


            Will return the XML elements with that XML tag, regardless of their location in the structure.


            Let me know if you need an XML rule script to do this (if it's something you do often).





            1 person found this helpful
            • 3. Re: Need to create XML attribute
              norton35 Level 1

              Thank you both for your very prompt replies. Both your suggestions are admirable, but I really don't know where to start constructing the script! Perhaps either of you could give me a hand with this?


              The XML structure is Root, Company (Attr 'companyID', 'pageNo'), Name, Address etc ... It's fairly simple. I understand the logic of both your suggestions, but I'm afraid I don't have the necessary knowledge to construct the proper syntax.


              I use XML and XSLT a lot to export and import data into FileMaker, but so far, I've never had to use XML Rules ... but I'm willing to learn!


              Once again, many, many thanks for your assistance ...



              • 4. Re: Need to create XML attribute
                norton35 Level 1

                Dave: I can create attributes using  XML Rules, but I'm afraid I still don't understand how to extract the Page Numbers from my text stream. This example script creates an attribute in the correct element (Company) and fills it with the contents of a child element (Head). But I can't figure out how to replace the contents with the page number on which the parent element (Company) resides:


                #include "glue code.jsx"
                function main(){
                    if (app.documents.length != 0){
                        var myDocument = app.documents.item(0);
                        var myRuleSet = new Array (new AddAttribute);
                            var elements = xmlElements;
                            __processRuleSet(elements.item(0), myRuleSet);
                        alert("No open document");
                    function AddAttribute(){
                        this.name = "AddAttribute";
                        this.xpath = "//Company/Head";   
                        this.apply = function(myElement, myRuleProcessor){
                            myElement.parent.xmlAttributes.add("PageNo", myElement.texts.item(0).contents);
                            return true;