xslt - Russian doll to Venetian blind xsl transformation -
i have been trying write xslt transformation convert 'russian doll' style xsd 'venetian blind' one.
i have written not work intended. have following xsd document:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:tns="gx" targetnamespace="gx" elementformdefault="qualified" attributeformdefault="unqualified"> <xs:complextype name="toptype"> <xs:sequence> <xs:element name="requestor" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="return_cancelled_customers" nillable="false" minoccurs="0" maxoccurs="1"> <xs:complextype> <xs:sequence> <xs:element name="external_customer_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="on_behalf_of" type="xs:string" nillable="false" minoccurs="1" maxoccurs="1"/> <xs:element name="msisdn_aaa" nillable="false" minoccurs="0" maxoccurs="1"> <xs:complextype> <xs:sequence> <xs:element name="gsp_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn_bbb" nillable="false" minoccurs="0" maxoccurs="1"> <xs:complextype> <xs:sequence> <xs:element name="sspp_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn_ccc" nillable="false" minoccurs="0" maxoccurs="1"> <xs:complextype> <xs:sequence> <xs:element name="sspc_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn_ddd" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="imsi" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> </xs:schema>
which try transform in to:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:tns="gx" targetnamespace="gx" elementformdefault="qualified" attributeformdefault="unqualified"> <xs:complextype name="toptype"> <xs:sequence> <xs:element name="requestor" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="return_cancelled_customers" type="tns:return_cancelled_customerstype" nillable="false" minoccurs="0" maxoccurs="1"/> </xs:sequence> </xs:complextype> <xs:complextype name="return_cancelled_customerstype"> <xs:sequence> <xs:element name="external_customer_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="on_behalf_of" type="xs:string" nillable="false" minoccurs="1" maxoccurs="1"/> <xs:element name="msisdn_aaa" type="tns:msisdn_aaatype" nillable="false" minoccurs="0" maxoccurs="1"/> </xs:sequence> </xs:complextype> <xs:complextype name="msisdn_aaatype"> <xs:sequence> <xs:element name="gsp_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn_bbb" type="tns:msisdn_bbbtype" nillable="false" minoccurs="0" maxoccurs="1"/> </xs:sequence> </xs:complextype> <xs:complextype name="msisdn_bbbtype"> <xs:sequence> <xs:element name="sspp_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn_ccc" type="tns:msisdn_ccctype" nillable="false" minoccurs="0" maxoccurs="1"/> </xs:sequence> </xs:complextype> <xs:complextype name="msisdn_ccctype"> <xs:sequence> <xs:element name="sspc_id" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn_ddd" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="imsi" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> <xs:element name="msisdn" type="xs:string" nillable="false" minoccurs="0" maxoccurs="1"/> </xs:sequence> </xs:complextype> </xs:schema>
the xslt writing work in progress , struggling ... did did similar thing , offer piece of advice? stuck.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes='exsl'> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tns="gx" targetnamespace="gx" elementformdefault="qualified" attributeformdefault="unqualified"> <!-- processing starts here root element --> <xsl:apply-templates/> </xs:schema> </xsl:template> <xsl:template match="xs:complextype[not(@name)]"> <xs:complextype name="{../@name}type"> <xs:sequence> <xsl:apply-templates/> <xs:element>current:<xsl:value-of select="./@name"/> parent: <xsl:value-of select="../@name"/> </xs:element> </xs:sequence> </xs:complextype> <xs:element>no name = parent: <xsl:value-of select="../@name"/> </xs:element> </xsl:template> <!--find nested complex type i.e. there no more child complext types--> <xsl:template match="xs:complextype[not(xs:sequence/xs:element/xs:complextype)]"> <xs:complextype name="{../@name}type"> <xsl:copy-of select="child::node()"/> <xs:element>current:<xsl:value-of select="./@name"/> parent: <xsl:value-of select="../@name"/>no more nesting!!!!!!!!!!!!!</xs:element> </xs:complextype> </xsl:template> <xsl:template match="xs:element"> <xsl:copy> <xsl:apply-templates select="attribute()"/> </xsl:copy> </xsl:template> <xsl:template match="attribute()"> <xsl:copy/> </xsl:template> <xsl:template match="xs:element[xs:complextype]"> <xs:element name="{@name}" type="{@name}type" nillable="{@nillable}" minoccurs="{@minoccurs}" maxoccurs="{@maxoccurs}"/> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
you start off, in root template, selecting of complex types in xsd
<xsl:apply-templates select="//xs:complextype" />
you have template match complex types, don't have name, can output them name based on parent element.
<xsl:template match="xs:complextype[not(@name)]"> <xs:complextype name="{../@name}type"> <xsl:apply-templates/> </xs:complextype> </xsl:template>
finally, have template match elements complex type, , add type attribute
<xsl:template match="xs:element[xs:complextype]"> <xs:element type="{@name}type"> <xsl:apply-templates select="@*" /> </xs:element> </xsl:template>
here full xslt
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes='exsl'> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tns="gx" targetnamespace="gx" elementformdefault="qualified" attributeformdefault="unqualified"> <!-- processing starts here root element --> <xsl:apply-templates select="//xs:complextype" /> </xs:schema> </xsl:template> <xsl:template match="xs:complextype[not(@name)]"> <xs:complextype name="{../@name}type"> <xsl:apply-templates/> </xs:complextype> </xsl:template> <xsl:template match="xs:element[xs:complextype]"> <xs:element type="{@name}type"> <xsl:apply-templates select="@*" /> </xs:element> </xsl:template> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> </xsl:stylesheet>
when applied xsd xml following output
<xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:tns="gx" targetnamespace="gx" elementformdefault="qualified" attributeformdefault="unqualified"> <xs:complextype name="toptype"> <xs:sequence> <xs:element maxoccurs="1" minoccurs="0" name="requestor" nillable="false" type="xs:string"/> <xs:element type="return_cancelled_customerstype" maxoccurs="1" minoccurs="0" name="return_cancelled_customers" nillable="false"/> </xs:sequence> </xs:complextype> <xs:complextype name="return_cancelled_customerstype"> <xs:sequence> <xs:element maxoccurs="1" minoccurs="0" name="external_customer_id" nillable="false" type="xs:string"/> <xs:element maxoccurs="1" minoccurs="1" name="on_behalf_of" nillable="false" type="xs:string"/> <xs:element type="msisdn_aaatype" maxoccurs="1" minoccurs="0" name="msisdn_aaa" nillable="false"/> </xs:sequence> </xs:complextype> <xs:complextype name="msisdn_aaatype"> <xs:sequence> <xs:element maxoccurs="1" minoccurs="0" name="gsp_id" nillable="false" type="xs:string"/> <xs:element type="msisdn_bbbtype" maxoccurs="1" minoccurs="0" name="msisdn_bbb" nillable="false"/> </xs:sequence> </xs:complextype> <xs:complextype name="msisdn_bbbtype"> <xs:sequence> <xs:element maxoccurs="1" minoccurs="0" name="sspp_id" nillable="false" type="xs:string"/> <xs:element type="msisdn_ccctype" maxoccurs="1" minoccurs="0" name="msisdn_ccc" nillable="false"/> </xs:sequence> </xs:complextype> <xs:complextype name="msisdn_ccctype"> <xs:sequence> <xs:element maxoccurs="1" minoccurs="0" name="sspc_id" nillable="false" type="xs:string"/> <xs:element maxoccurs="1" minoccurs="0" name="msisdn_ddd" nillable="false" type="xs:string"/> <xs:element maxoccurs="1" minoccurs="0" name="imsi" nillable="false" type="xs:string"/> <xs:element maxoccurs="1" minoccurs="0" name="msisdn" nillable="false" type="xs:string"/> </xs:sequence> </xs:complextype> </xs:schema>
Comments
Post a Comment