我一直在尝试编写一个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>
答案 0 :(得分:4)
您可以在根模板中开始,只需在xsd中选择所有复杂类型
<xsl:apply-templates select="//xs:complexType" />
然后,您将拥有一个模板来匹配没有名称的复杂类型,因此您可以使用基于其父元素的名称输出它们。
<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>
这是完整的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 from the 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>
当应用于XSD XML时,输出以下内容
<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>