Page 1 of 1

<w:instrText> tags represented as Text objects

PostPosted: Fri Aug 18, 2017 1:20 am
by tommci14
Currently <w:instrText> tags are represented as Text objects. This makes them impossible to distinguish from <w:t> elements that actually contain field values, rather than formatting declarations.

This is compounded by the fact that the <w:fldChar w:fldCharType="separate"/> tag separating formatting from values is optional, so can't be used to make this distinction either.

Can <w:instrText> be represented by a dedicated type?

Re: <w:instrText> tags represented as Text objects

PostPosted: Fri Aug 18, 2017 7:47 am
by jason
If you look at the wml ObjectFactory, you'll see:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Text }{@code >}}
     *
     */

    @XmlElementDecl(namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", name = "instrText", scope = R.class)
    public JAXBElement<Text> createRInstrText(Text value) {
        return new JAXBElement<Text>(_RInstrText_QNAME, Text.class, R.class, value);
    }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


In other words, w:instrText is represented by JAXBElement<Text>, and that object "knows" it is instrText, via QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "instrText");

It is only when you "unwrap" (XmlUtils.unwrap) the JAXBElement that you are left with a bare Text element. The traversal code does this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        /**
         * If an object is wrapped in a JAXBElement, return the object.
         * Warning: be careful with this. If you are copying objects
         * into your document (rather than just reading them), you'll
         * probably want the object to remain wrapped (JAXB usually wraps them
         * for a reason; without the wrapper, you'll (probably?) need an
         * @XmlRootElement annotation in order to be able to marshall ie save your
         * document).
         *
         * @param o
         * @return
         */

        public static Object unwrap(Object o) {
               
                if (o==null) return null;
               
                if (o instanceof javax.xml.bind.JAXBElement) {
                        log.debug("Unwrapped " + ((JAXBElement)o).getDeclaredType().getName() );
                        log.debug("name: " + ((JAXBElement)o).getName() );
                        return ((JAXBElement)o).getValue();
                } else {
                        return o;
                }
               
        }
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


This (JAXBElement) is the pattern which is followed wherever the underlying XML schemas use a complex type for several elements. In this case:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <xsd:complexType name="CT_Text">
                <xsd:annotation>
                        <xsd:appinfo>
                                <jaxb:class name="Text"/>
                        </xsd:appinfo>
                </xsd:annotation>
                <xsd:simpleContent>
                       
                        <xsd:extension base="ST_String">
                                <xsd:attribute ref="xml:space" use="optional">
                                        <xsd:annotation>
                                                <xsd:documentation>Content Contains Significant
                                                        Whitespace</xsd:documentation>
                                        </xsd:annotation>
                                </xsd:attribute>
                        </xsd:extension>
                </xsd:simpleContent>
        </xsd:complexType>

                        <xsd:element name="instrText" type="CT_Text">
                                <xsd:annotation>
                                        <xsd:documentation>Field Code</xsd:documentation>
                                </xsd:annotation>
                        </xsd:element>


 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4