xml - Parameterize allowed numeric range or embed in type name? -


from have seen now, problem might not possible solve within given constraints, i'll give chance here.

i trying write xsd(1.1)-file matching pretty simple xml. problem existence of attributes within xml tags, forced me use xs:extension. example of element:

<xs:element name="index">     <xs:complextype>         <xs:simplecontent>             <xs:extension base="int1to10">                 <xs:attribute name="comment" fixed="attribute in xml tag"/>             </xs:extension>         </xs:simplecontent>     </xs:complextype> </xs:element> 

whereas type created called "int1to10" looks this:

<xs:simpletype name="int1to10">     <xs:restriction base="xs:integer">         <xs:mininclusive value="1"/>         <xs:maxinclusive value="10"/>     </xs:restriction> </xs:simpletype> 

this work, problem being lot of writing , if want change something, need change reference name stay consistent values in defined type. if small file, doable; there quite few elements here, can have pretty combination of min- , max-integers. each new element has check if type exists already, or has create it. feels incredibly redundant.

what i'm looking simple solution along lines of:

<xs:element>...     <xs:extension base="xs:integer" mininclusive="1" maxinclusive="10">         <xs:attribute name="comment" fixed="attribute in xml tag"/>     </xs:extension>... </xs:element>     

though found, not seem possible. in this stackoverflow post example accepted answer stated way i'm doing way go; though post little old, maybe there has been recent breakthrough missed?

the other option can come restructuring xml file itself, (current)

<index comment="attribute in xml tag">(some value between 1 , 10)</index> 

to

<index>     <value>(some value between 1 , 10)</value>     <comment>attribute in own tag</comment> </index> 

which require quite work , change of other things work xml (scripts etc.), rather avoid that.

so wrap up: there way not write new simpletype each new integer combination elements can have? suggestions welcome, maybe there entirely different approach or xsd-structure possible?

i think you've presented way go.

however, may ...

  • use consistent naming scheme int_<lower_bound>_to_<upper_bound> range types , autogenerate matching xsd type declarations template using text processing tool independent xml schemas.

  • transform syntax came (<xs:extension base="xs:integer" mininclusive="1" maxinclusive="10"> using xslt.

poc

generate (perl)

writes file containing xml fragment consisting of systematically generated type definitions.

use io::file; use warnings; use strict;  ($dict, $fh_out, $fpath, $instance); $tmpl =<<eotmpl; <xs:simpletype name="int\${lb}to\${ub}">     <xs:restriction base="xs:integer">         <xs:mininclusive value="\${lb}"/>         <xs:maxinclusive value="\${ub}"/>     </xs:restriction> </xs:simpletype> eotmpl  %dict = ( 'lb' => 0,  'ub' => 0 ); $fpath = "<wherever>"; $fh_out = new io::file ( ">" . $fpath );  ($dict{lb} = 1; $dict{lb} <= 4; $dict{lb}++ ) {     ($dict{ub} = $dict{lb}+1; $dict{ub} <= 4; $dict{ub}++ ) {         ($instance = $tmpl) =~ s/\$\{([a-z]+)\}/$dict{$1}/g;         print $fh_out "$instance\n";     } }  $fh_out->close;  exit(0); 

transform (xslt)

transforms pseudo type specs source file full-fledged int<m>to<n> type declarations. not check duplicate definitions.

<?xml-stylesheet version="2.0" encoding="utf-8"?> <!--     t.xslt     xsd typespec generation --> <xsl:stylesheet xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0">     <!--         specifying output     -->     <xsl:output method="xml" encoding="utf-8" indent="yes"/>      <!-- identity transform -->     <xsl:template match="@*|node()">         <xsl:apply-templates select="@*|node()"/>     </xsl:template>      <!--            toplevel template:     -->     <xsl:template match="/">         <xsl:message select="string('main template: start;')"/>         <mytypes xmlns:xs="http://www.w3.org/2001/xmlschema">             <xsl:apply-templates />         </mytypes>         <xsl:message select="string('main template: end;')"/>     </xsl:template>      <!--            type subst     -->     <xsl:template match="xs:element/xs:complextype/xs:simplecontent/xs:extension[@base='xs:integer'][@mininclusive][@maxinclusive]">         <xs:simpletype>             <xsl:attribute name='name' select="concat('int', string(@mininclusive), 'to', string(@maxinclusive))"/>             <xs:restriction base="xs:integer">                 <xs:mininclusive><xsl:attribute name='value' select="number(@mininclusive)"/></xs:mininclusive>                 <xs:maxinclusive><xsl:attribute name='value' select="number(@maxinclusive)"/></xs:maxinclusive>             </xs:restriction>         </xs:simpletype>     </xsl:template> </xsl:stylesheet> 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -