Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to parse an XML file taking a reference to its xsd file. The xsd file contains some optional attribute with a default value being provided. I have to parse the xml file in such a way so that if an optional attribute is not present in the xml, I should be able to fetch the default value for that the attribute that are not present in the XML(from xsd). The issue is that I couldn't find any appropriate parser in perl for this Job.I could find a parser that is able to validate the file but couldn't get the default value of attributes that are not present the XML. Can you please suggest me any parser/module that can help me in getting the required data. Thanks
  • Comment on XML parser to fetch default values of the optional attributes from the xsd file

Replies are listed 'Best First'.
Re: XML parser to fetch default values of the optional attributes from the xsd file
by roboticus (Chancellor) on Jan 10, 2011 at 20:09 UTC

    Just use a standard XML parser to parse the XSD and the XML document. If you don't find the value in the XML document, then you can return the default value from the XSD. Alternatively, write an XSLT to add the default values into the document.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      then you can return the default value from the XSD

      One would first need to identify the relevant schema definition for a given element or attribute. How do you propose to do that?

        ikegami:

        I assumed that if he (a) could detect that the defaults are missing, and (b) can track down the correct values, that he already knows how to identify the correct values.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

      write an XSLT to add the default values into the document

      Are you serious?!

Re: XML parser to fetch default values of the optional attributes from the xsd file
by dHarry (Abbot) on Jan 11, 2011 at 12:27 UTC

    Anonymous Monk

    Did you ask about XML::Xerces earlier?

    The issue is that I couldn't find any appropriate parser in perl for this Job

    I don't think there is one. Any compliant parser should do the job though. There are many parsers around, but only few are fully conformant. My one-stop solution is xerces. If you are willing to spend money there are excellent commercial parsers on the market. For the sake of clarity: you need a parser that fully supports the XMLSchema recommendation, anything else won't do.

    When an attribute is declared with a default value in the schema and you parse/process the instance document, the value of the attribute returned by the parser will be:

    • - if the instance document contains a value for the attribute this value is taken
    • - else the default value as defined in the schema is taken

    No need to use XSLT or anything.

    Cheers

    Harry