XSD 到 XML 转换器

问题描述 投票:0回答:3

我正在尝试将 XSD 文件转换为其相应的 XML 文档。使用 IntelliJ 的内置工具使这一切变得简单,但有些问题。它似乎正确地创建了所有内容,这是一个很大的 XSD,但仔细检查后,会发现缺少一些元素。我使用的具体 XSD 来自此处:https://www.iata.org/contentassets/76a4543036794d59ad747f3d80bb9792/19.2-schemas-edist.zip。文件名为 IATAAirShoppingRS.xsd。
这是该文件的一个片段。在 CarrierOffersSummary 之后,应该会创建和扩展更多元素,但只有注释。

<iata:TotalAmount CurCode="token">1000.00</iata:TotalAmount>
          </iata:TotalPrice>
          <!--Optional:-->
          <iata:ValidatingCarrierCode>token</iata:ValidatingCarrierCode>
          <!--Optional:-->
          <iata:WebAddressURL>http://www.corp.com/pater/regina</iata:WebAddressURL>
        </iata:ALaCarteOffer>
        <!--Optional:-->
        <iata:CarrierOffersSummary>
          <!--Optional:-->
          <iata:HighestOfferPrice>
            <!--Optional:-->
            <iata:LoyaltyUnitAmount CurCode="token">1000.00</iata:LoyaltyUnitAmount>
            <!--Optional:-->
            <iata:LoyaltyUnitName>token</iata:LoyaltyUnitName>
            <!--Optional:-->
            <iata:TotalAmount CurCode="token">1000.00</iata:TotalAmount>
          </iata:HighestOfferPrice>
          <!--Optional:-->
          <!--Optional:-->
        </iata:CarrierOffersSummary>
        <!--Zero or more repetitions:-->
        <!--Zero or more repetitions:-->
      </iata:CarrierOffers>
    </iata:OffersGroup>
    <!--Optional:-->
    <!--Zero or more repetitions:-->
    <!--Zero or more repetitions:-->
    <!--Optional:-->
    <!--Zero or more repetitions:-->

我感谢您提供的任何帮助。

xml intellij-idea xsd converters
3个回答
0
投票

我假设您指的是“零或更多重复”评论?

我没有使用过 IntelliJ 的工具,但 Eclipse XML 示例文档生成器具有控制输出的设置,包括是否为可选 (minOccurs="0") 元素生成任何内容。我建议您查看IntelliJ工具的文档,看看它是否有类似的设置。 然而...

我正在尝试将 XSD 文件转换为其相应的 XML 文档。

对于大多数 XSD,尤其是大型且复杂的 XSD,不存在这样的事情。任何重要的 XSD 都描述了整个 XML 文档系列。如果您使用此工具生成单元测试数据,那么要非常小心功能覆盖率。您不应该相信任何自动化工具可以为您的测试生成有用的数据 - 这需要了解您项目的业务需求。


0
投票

正如 @kimbert 指出的那样,发生这种情况是因为 xsd 文件允许这些字段出现零次,而生成器通过采取该路线而变得懒惰。强制 Intellij 为每个字段生成至少一个元素的快速而肮脏的解决方案是创建 xsd 的副本,并在该副本中将所有出现的

minOccurs="0"
替换为
minOccurs="1"
,这对于 Intellij 来说很容易替换文件中的所有内容工具。然后,您可以从新的 xsd 生成 xml,并且将为每个字段至少创建一个条目。


0
投票

你不能使用自己的模板来做到这一点吗?并使用 xsltproc 运行它

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<!-- Match a xsd:schema node -->
<xsl:template match="xsd:schema">
    <xsl:apply-templates select="xsd:element"/>
</xsl:template>
<!-- Match the root element of the input XML -->

<xsl:template match="/xsd:schema/xsd:element[xsd:complexType]">
    <xsl:element name="{@name}">
    <!-- Process the complexType that would be the root node -->
        <xsl:apply-templates select="node()//xsd:sequence" mode="nested"/>
    </xsl:element>
</xsl:template>


<!-- Match a xsd:complexType node -->
<xsl:template match="xsd:complexType" mode="nested">
    <xsl:element name="{@name}">
        <xsl:apply-templates select="xsd:sequence/*" mode="nested"/>
    </xsl:element>
</xsl:template>

<!-- Match a xsd:simpleType node -->
<xsl:template match="xsd:simpleType" mode="nested">
    <xsl:element name="{@name}">
        <xsl:apply-templates select="xsd:restriction" mode="nested"/>
    </xsl:element>
</xsl:template>

<!-- Match children elements of xsd:sequence -->
<xsl:template match="xsd:element" mode="nested">
    <xsl:element name="{@name}">
    <xsl:choose>
  <xsl:when test="starts-with(@type,'xsd')">
  </xsl:when>
  <xsl:otherwise>
      <xsl:apply-templates select="//xsd:simpleType[@name = current()/@type]/*" mode="nested"/>
      <xsl:apply-templates select="//xsd:complexType[@name = current()/@type]/*" mode="nested"/>
  </xsl:otherwise>
</xsl:choose>
    </xsl:element>

</xsl:template>

<!-- Match a restriction node -->
<xsl:template match="xsd:restriction" mode="nested">
        <xsl:apply-templates select="xsd:enumeration" mode="nested"/>
</xsl:template>

<!-- Match an enumeration node -->
<xsl:template match="xsd:enumeration" mode="nested">
    <xsl:value-of select="@value"/>
     <xsl:if test="following-sibling::node()">
    <xsl:text>
    </xsl:text>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.