6 Replies Latest reply on Mar 29, 2011 5:56 PM by mick.powell

    Trouble with Namespaces in e4x

    mick.powell

      Hello All,

       

      Not sure what's going on with this site - but when I searched for previous discussions on xml or e4x I got 0???

       

      I'm trying to generate a styles.xml file for a document to be read in the Open Office open document formats. I have added all the namespaces into an XML variable on the first line, then added a child for font declarations. At this point the XML looks like this:

       

      <document-styles xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="urn:oasis:names:tc:opendocument:xmlns:office:1.0">
        <font-face-decls/>
      </document-styles>

       

      at this point in the code if I trace this:

       

      trace(xmlBody.child("font-face-decls").inScopeNamespaces());

       

      I get an array with my list of namespaces. If I trace this:

       

      trace(xmlBody.child("font-face-decls").namespace("style").prefix);

       

      I get : style

       

      But, if I try this:

       

      xmlBody.child("font-face-decls").addChild(<style:font-face/>);

       

      I get prefix unbound - but the prefix patently is bound as shown above (this might also be a problem with the hyphen though maybe?).

       

      This works:

       

      xmlBody.child("font-face-decls")[0].styleNS::["font-face"] = "";

       

      But this doesn't (i get a tern is undefined error)

       

      xmlBody.child("font-face-decls")[1].styleNS::["font-face"] = "";

       

      What I'm after is something like this:

       

      <font-face-decls>
        <style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss" style:font-pitch="variable"/>
        <style:font-face style:name="Arial Unicode MS" svg:font-family="&apos;Arial Unicode MS&apos;" style:font-family-generic="system" style:font-pitch="variable"/>
        <style:font-face style:name="SimSun" svg:font-family="SimSun" style:font-family-generic="system" style:font-pitch="variable"/>
        <style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-family-generic="system" style:font-pitch="variable"/>
      <font-face-decls>

       

      Does anybody out there know how to work this?  I'm really at the end of my tether!

       

      Cheers!

        • 1. Re: Trouble with Namespaces in e4x
          Flex harUI Adobe Employee

          In your code:

           

              xmlBody.child("font-face-decls").addChild(<style:font-face/>);

           

          You are defining a completely new XML chunk in:

           

              <style:font-face/>

           

          Which hopefully makes it more obvious that the style prefix is unbound in

          that chunk.

           

          You can define the prefix in that chunk, or try using "default xml

          namespace".  See the doc for usage.

          • 2. Re: Trouble with Namespaces in e4x
            mick.powell Level 1

            Thanks for the reply Alex - your stuff out in the world has helped me a lot in the past - good to be able to thank you for that.

             

            But this time I'm not really understanding you...

             

            I'm already using the default namespace in the XML body for the "office" namespace (in the open office generated styles.xml it uses office:document-styles, office:font-face-decls). So office in this example is already my default namespace, and has been set in as using

             

            default xml namespace = officeNS;

             

            As i'm adding a child I was assuming that because the style namespace is declared at this point in the XML (and the traces prove that), why wouldn't it accept "style". Its not really a new chunk of xml, its an additional child chunk of xml surely? Just as an additional point on that as well - why does the alternative method actually work when I set child[0]? and not work when I set child[1]? I was more assuming this was really my issue here.

            • 3. Re: Trouble with Namespaces in e4x
              Flex harUI Adobe Employee

              The child you are adding is a standalone snippet of XML and doesn't have its

              own set of namespace prefixes.  If you pull out an existing child and call

              toXMLString() on it, you'll see that it has prefixes defined in it, but your

              proposed child doesn't.

              1 person found this helpful
              • 4. Re: Trouble with Namespaces in e4x
                mick.powell Level 1

                I think I'm with what you're saying, the XML snippet inside of Actionscript generates the error right? I added the <font-face-decls> using the addChild method and I'm supposing that now this is part of the main block of XML, it now registers the namespace, and that's why I can trace that. Whereas addChild("<style:font-face>") triggers the error where it is between the brackets becasue its not part of the main xml block yet...

                 

                Do you have any thoughts on the alternative example:

                 

                xmlBody.child("font-face-decls")[0].styleNS::["font-face"] = "";

                 

                This works, but only once (ie [1] throws an error so it seems I can't get multiple children this way).

                • 5. Re: Trouble with Namespaces in e4x
                  mick.powell Level 1

                  And just to be clear - this is the final example of the XML block before trying to add the second line of the font declarations:

                   

                  <document-styles xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="urn:oasis:names:tc:opendocument:xmlns:office:1.0">
                    <font-face-decls>
                      <style:font-face svg:font-family="Tahoma" style:font-family-generic="system" style:font-pitch="variable"/>
                    </font-face-decls>
                  </document-styles>

                   

                  The extra attribute tags are set using the same method ie:

                   

                  xmlBody.child("font-face-decls")[0].styleNS::["font-face"].@svgNS::["font-family"] = family;

                   

                  etc.

                  • 6. Re: Trouble with Namespaces in e4x
                    mick.powell Level 1

                    OK I see this now. I didn't want to add the prefix again as I've already added it once and I didn't want namespace declarations all over the XML. I can see now that once I add the new XML with the namespace redifined in that line of XML (cumbersome stuff!), it won't actually reproduce the namespace declaration if it already finds it in the main body XML. So I can just build up the XML string for the appendChild including the attributes and the namespaces I need and just bang it in once for each font.

                     

                    Thanks!