如何从 Perl 中的示例 XML 文档创建模式? [英] How can I create a schema from an example XML document in Perl?
问题描述
我需要基于 XML 文件创建 XSD 架构.是否有任何 Perl 模块可以做到这一点?
I need to create a XSD schema based on a XML file. Are there any Perl modules which can do this?
推荐答案
您可以使用任何 XSLT 处理器通过 XSL 转换创建 XSD.参见 XML::XSLT
You can create the XSD by a XSL transformation using any XSLT processor. See XML::XSLT
XSD 文件包含两种元素类型:简单和复杂.所有叶节点都必须转换为简单类型元素,而其他叶节点必须转换为复杂类型.叶节点是没有任何后代的节点.对应的 XPath 是 //*[not(descendant::element())]
.下面的 XSLT 实现了这个方法:
An XSD file contains two element types: simple and complex. All leaf nodes have to be translated into simple type elements and the others have to be translated into complex types. Leaf nodes are nodes without any descendants. The corresponding XPath is //*[not(descendant::element())]
. The following XSLT implements this approch:
<?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">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsl:for-each select="//*[not(descendant::element())]">
<xsl:element name="xs:element">
<xsl:attribute name="name">
<xsl:value-of select="name()"/>
</xsl:attribute>
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="//*[descendant::element()]">
<xsl:element name="xs:element">
<xsl:attribute name="name">
<xsl:value-of select="name()"/>
</xsl:attribute>
<xs:complexType>
<xs:sequence>
<xsl:for-each select="child::*">
<xsl:element name="xs:element">
<xsl:attribute name="ref">
<xsl:value-of select="name()"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xs:sequence>
</xs:complexType>
</xsl:element>
</xsl:for-each>
</xs:schema>
</xsl:template>
</xsl:stylesheet>
以下示例:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<firstname>Peter</firstname>
<lastname>Pan</lastname>
<born>
<year>1904</year>
<month>12</month>
<day>27</day>
</born>
</person>
将产生以下架构:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="firstname">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="lastname">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="year">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="month">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="day">
<xs:simpleType>
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element ref="firstname"/>
<xs:element ref="lastname"/>
<xs:element ref="born"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="born">
<xs:complexType>
<xs:sequence>
<xs:element ref="year"/>
<xs:element ref="month"/>
<xs:element ref="day"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
这篇关于如何从 Perl 中的示例 XML 文档创建模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!