gpt4 book ai didi

xslt - 俄罗斯娃娃到百叶窗xsl改造

转载 作者:行者123 更新时间:2023-12-04 22:37:57 25 4
gpt4 key购买 nike

我一直在尝试编写一个 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>

最佳答案

您可以在根模板中开始,只需在 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>

关于xslt - 俄罗斯娃娃到百叶窗xsl改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15810773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com