Generating metadata with XSLT

I regularly create an XSD schema by transforming the legacy system's proprietary data model. It is very good. However, the legacy system allows me to specify very basic parameter attributes such as data type ( int

, string

etc.).

I would like to improve the XSL transformation with a mechanism that allows me to add metadata to provide more details about the transformation. I thought of something like Java property naming to assign XPath attributes.

Consider the following example:

system data stale model (actually neat but most suitable for demo purposes)

<datamodel>
  <customer>
    <firstName type="string"/>
    <lastName type="string"/>
    <age type="int">
  <customer>
</datamodel>

      

metadata

customer/firstName/@nillable=false
customer/lastName/@nillable=false
customer/age/@nillable=true
customer/firstName/@minOccurs=1
customer/firstName/@maxOccurs=1
customer/lastName/@minOccurs=1
customer/lastName/@maxOccurs=1

      

final XSD schema

...
<xs:complexType name="customerType">
  <xs:sequence>
    <xs:element name="firstName" type="xs:string" nillable="false" minOccurs="1" maxOccurs="1"/>
    <xs:element name="lastName" type="xs:string" nillable="false" minOccurs="1" maxOccurs="1"/>
    <xs:element name="age" type="xs:int" nillable="true"/>
  </xs:sequence>
</xs:complexType>
...

      

What do you think about this? Is there a way to include metadata in an XSL stylesheet?

+1


source to share


2 answers


The best solution would be to change the stale data by adding missing metadata.

An instance of the modified dictionary "datamodel" could be something like this:

<datamodel xmlns:nm="my:new.meta">
    <customer>
        <firstName type="string"
                   nm:nillable="false"
                   nm:minOccurs="1"
                   nm:maxOccurs="1"
                   />
        <lastName type="string"
                  nm:nillable="false"
                  nm:minOccurs="1"
                  nm:maxOccurs="1"
                  />
        <age type="int" nm:nillable="true"/>
    </customer>
</datamodel>

      

Putting new properties in a separate namespace is a good way to easily distinguish them from already supported properties. Generally, the use of attributes in a namespace is discouraged, so if this should be avoided, subelements (belonging to the new namespace) can be used instead of attributes. Adding new attributes to a different namespace can result in legacy schema validation not being rejected.

If for some reason it is impossible to change the outdated data, I would recommend not to include new properties in the XSLT stylesheet (this is quite possible, for example, as defining this as global content <xsl:variable>

), but to provide these new properties as a separate XML file or as a set from multiple XML files.



Any XML file can be accessed dynamically during XSLT transformation using the XSLT document () function. An instance of the XML file with new properties might look like this:

<newProperties xmlns:nm="my:new.meta">
    <customer>
        <firstName nm:nillable="false"
                   nm:minOccurs="1"
                   nm:maxOccurs="1"
                   />
        <lastName nm:nillable="false"
                  nm:minOccurs="1"
                  nm:maxOccurs="1"
                  />
        <age nm:nillable="true"/>
    </customer>
</newProperties>

      

Hope it helped.

Greetings,

Dimitar Novachev

+2


source


"What do you think about this?"

Two Three things.



  • Fix outdated metadata. It's XML. Add stuff to that. Add namespace if you need.

  • If you can't fix the stale metadata, who will keep the second set of metadata that isn't written in XML? Who will double-enter metadata changes? What are the chances of someone following?

  • Consider using XML for additional metadata rather than pseudo XPath. Consistency will help those who come after you figure out what's going on.

+1


source







All Articles