我一直在尝试编写一个 xslt 转换来将“俄罗斯娃娃”风格的 xsd 转换为“百叶窗”风格。
我写了一些东西,但这并不完全符合我的预期。所以我有以下xsd文档:
<?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>
我尝试将其转换为:
<?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>
我正在编写的 XSLT 正在进行大量工作,我正在苦苦挣扎……有没有人做过类似的事情并且可以提供一条建议?基本上我被卡住了。
<?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 from the 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 the most nested complex type i.e. there are 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>