XSLT/XML schema




  • I'm currently researching what to use for a project.

    The basic task is to be able to define in a simple way the following things:

    grouping of blocks                                (linking multiple blocks too eachother)
    defining validation schemes for blocks    (email validation, string matching, etc...)
    defining behaviour for blocks                  (text align types, img fitting types)
    defining behaviour for groups of blocks.  (float elements up)
    if then else structure.                           (if  empty:block_01 :: hide:block_02)
    defining type of input for blocks.            (text/enum/etc..)

     

    It's for a CMS system that defines custom template pages that can be filled in by others.

    So for instance you could have a contact page that has the following attributes.

    companyname
    slogan

    tel: tel
    mob: mob
    fax: fax
     

    Now the structure of the page can allready be defined by a existing system in a XML. However now we want to be able to make the dynamic elements be controlable by logic. For instance, if no "mob" is filled in by a user, "fax" should move up to the place where mob used to be. Also the value in tel and mob should be validated to a telephone nummber.

    Now i think this reeks XSLT + XML scema/relaxNG, however i'm worried i won't be able to express al the requirements into this. ideally i would want something where i could define it all in 1 structure.
    For instance take the following mock XML'ish definition.


    <element name="contactdata">
        <element name="firstname">
            <validation>
                <text/>
            </validation>    
        </element>
        <element name="jobtitle">
            <validation>
                <text/>
            </validation>    
        </element>
        <element name="contact_block" type="groupmacro">
            <element name="tel_block" type="groupmacro">
                <element name="block_text_tel" />
                <element name="tel">
                    <validation>
                        <not_empty/>
                        <telephone_international/>
                    </validation>
                </element>
                <behaviour/>
            </element>
            <element name="mob_block" type="groupmacro">
                <element name="block_text_mob" />
                <element name="mob">
                    <validation>
                        <telephone_international/>
                        <telephone_mobile/>
                    </validation>
                </element>
                <behaviour>
                    <if:mob:empty>
                        <hide>mob_block</hide>
                    </if>
                </behaviour>
            </element>
            <element name="fax_block" type="groupmacro">
                <element name="block_text_fax" />
                <element name="fax">
                    <validation>
                        <telephone_international/>
                    </validation>
                </element>
                <behaviour>
                    <if📠empty>
                        <hide>fax_block</hide>
                    </if>
                </behaviour>
            </element>
            <behaviour>
                <vertical-align>top</vertical-align>
            </behaviour>
        </element>
    </element>


    Now the kicker is that while it will initially be used to generate html document, in the future it should be able to also create other output types. Because of this the system should not assume that common HTML behaviour will occur. (like elements automatically floating up, because the item above it doesnt have any text)

    I'm currently reading into XSLT and XML schema, since i only have a very shallow understanding of those standards. But i was hoping someone here might have had experience with this kind of stuff, and could tell me if i'm even looking in the right direction.



     



  • I think as a basic principle you can say, XSLT/Scheme is the tool of choice if you need to manipulate XML data - that is the hierarchy of nodes and attributes and so on. On the other hand, XSLT completely fails at processing non-XML data (like e-mail adresses). It is possible but unnecessarily painful and complicated.

    So I'd say, if your input data already is in a clearly-structured "true" XML format and if you think you can use most of the actual text content inside (text elements, attribute values) directly, without further processing or validating, go for XSLT.
    However, if you have non-XML strings or a WTF application with "pseudo XML" a la "<data>some huge string in csv or a custom format</data>" go for regexes instead.

    If you want to use XSLT client-side, keep in mind too that there are still many browsers that haven't implemented it yet. So if you're programming for the 'net, you want to provide fallbacks.
    If you want to use it server-side you might also want to take a look at XQuery, a similar technology that could be easier to use.


     



  • Word of the wise: if you want XML data specification, do NOT use XML Schemas.

    Ever.

    They're verbose, annoying, and they will trip you time and time again, plus if you ever try to handle multiple namespaces you're hosed.

    Go for RelaxNG instead.


Log in to reply