本文介绍了如何手动使用XSLT生成XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为这个问题而苦苦挣扎。我可以使用XSLT根据输入的XML文件生成XSD吗?
我知道我可以使用许多软件来自动执行此操作,但是我需要通过编写代码来实现。

I am struggling with this question. Could I, using XSLT, generate an XSD based on an XML file input ?I know there are many software that I can use to do that automatically, but I need it by writing code.

您能帮助我如何开始

这是一个示例XML文件:我需要使用XSLT生成XSD并对其进行验证:

This is a sample XML file: I need to generate XSD using XSLT and validate it:

<test>
  <a>
    <b> </b>
  </a>
  <d> </d>
</test>


推荐答案

正如Marcus在评论中所观察到的,有很多程度自由,但这是您的起点:

As Marcus observed in the comments, there are many degrees of freedom, but here's a start for you:

您的输入XML:

<test>
  <a>
    <b> </b>
  </a>
  <d> </d>
</test>

鉴于此XSLT:

<xsl:stylesheet version="2.0"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xsl:apply-templates/>
    </xs:schema>
  </xsl:template>
  <xsl:template match="*[*]">
    <xs:element name="{local-name()}">
      <xs:complexType>
        <xs:sequence>
          <xsl:apply-templates select="*"/>
        </xs:sequence>
        <xsl:apply-templates select="@*"/>
      </xs:complexType>
    </xs:element>
  </xsl:template>
  <xsl:template match="*[not(*) and text()]">
    <xs:element name="{local-name()}">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xsl:apply-templates select="@*"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xsl:template>
  <xsl:template match="@*">
    <xs:attribute name="{local-name()}"/>
  </xsl:template>
</xsl:stylesheet>

此XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="test">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="a">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="b">
                        <xs:complexType>
                           <xs:simpleContent>
                              <xs:extension base="xs:string"/>
                           </xs:simpleContent>
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
            <xs:element name="d">
               <xs:complexType>
                  <xs:simpleContent>
                     <xs:extension base="xs:string"/>
                  </xs:simpleContent>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>



奖金



您的输入添加了属性的XML:

<test w="w1">
  <a x="x1" y="y1">
    <b z="z1"> </b>
  </a>
  <d> </d>
</test>

此XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="test">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="a">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="b">
                        <xs:complexType>
                           <xs:simpleContent>
                              <xs:extension base="xs:string">
                                 <xs:attribute name="z"/>
                              </xs:extension>
                           </xs:simpleContent>
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
                  <xs:attribute name="x"/>
                  <xs:attribute name="y"/>
               </xs:complexType>
            </xs:element>
            <xs:element name="d">
               <xs:complexType>
                  <xs:simpleContent>
                     <xs:extension base="xs:string"/>
                  </xs:simpleContent>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
         <xs:attribute name="w"/>
      </xs:complexType>
   </xs:element>
</xs:schema>

左为练习:


  • 生成除俄罗斯娃娃之外的XSD样式。

  • 处理名称空间。

  • 在可能的范围内加强键入字符串基元。

  • ...

  • Generate XSD styles other than Russian Doll.
  • Handle namespaces.
  • Tighten typing where possible beyond string primitives.
  • ...

这篇关于如何手动使用XSLT生成XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 20:01
查看更多