0

我有以下 XML 文件:

<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38">
 <my:Text1></my:Text1>
 <my:Group>
  <my:Text2></my:Text2>
 </my:Group>
</my:myFields>

以及它的 XSD 定义:

<xsd:schema targetNamespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="myFields">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element ref="my:Text1" minOccurs="0"/>
    <xsd:element ref="my:Group" minOccurs="0" maxOccurs="unbounded" />
   </xsd:sequence>
   <xsd:anyAttribute processContents="lax" namespace="http://www.w3.org/XML/1998/namespace"/>
  </xsd:complexType>
 </xsd:element>
 <xsd:element name="Text1" type="xsd:string"/>
 <xsd:element name="Group">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element ref="my:Text2" minOccurs="0"/>
   </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="Text2" type="xsd:string"/>
 </xsd:element>
</xsd:schema>

我想创建以下基于 XML 和 XSD 的 XML 文件:

<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38">
 <my:Text1 type="string" minOccurs="0"></my:Text1>
 <my:Group minOccurs="0" maxOccurs="unbounded">
  <my:Text2 type="string" minOccurs="0"></my:Text2>
 </my:Group>
</my:myFields>

使用 .NET 平台最简单的方法是什么?是否可以使用 XSLT 转换?

4

3 回答 3

2

给定一个正确的模式

<xsd:schema targetNamespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="myFields">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="my:Text1" minOccurs="0"/>
                <xsd:element ref="my:Group" minOccurs="0" maxOccurs="unbounded" />
            </xsd:sequence>
            <xsd:anyAttribute processContents="lax" namespace="http://www.w3.org/XML/1998/namespace"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="Text1" type="xsd:string"/>
    <xsd:element name="Text2" type="xsd:string"/>
    <xsd:element name="Group">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="my:Text2" minOccurs="0"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

以及您指定的 XML 输入,此样式表

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ext="http://exslt.org/common" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38">
<xsl:output method="xml" indent="yes"/>
    <xsl:variable name="schema" select="document('so.xsd')//xsd:schema"/>

<xsl:template match="/">
     <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
</xsl:template>

<xsl:template match="my:*">
    <xsl:variable name="name" select="name()"></xsl:variable>
    <xsl:variable name="basename" select="substring-after($name,':')"></xsl:variable>
    <xsl:copy>
        <xsl:variable name="types" select="$schema//xsd:element[@name=$basename]/@type"/>
        <xsl:if test="$types">
            <xsl:attribute name="type">
                <xsl:value-of select="$types"/>
            </xsl:attribute>
        </xsl:if>
        <xsl:for-each select="$schema//xsd:element[@ref=$name]/@*[name()!='ref']">
            <xsl:attribute name="{name()}">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates select="*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="node()|@*">
    <xsl:copy><xsl:apply-templates select="node()|@*" /></xsl:copy>
</xsl:template>

</xsl:stylesheet>

产生以下输出,我相信这是您想要的。

<?xml version="1.0" encoding="utf-8"?>
<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003">
   <my:Text1 type="xsd:string" minOccurs="0"/>
   <my:Group minOccurs="0" maxOccurs="unbounded">
      <my:Text2 type="xsd:string" minOccurs="0"/>
   </my:Group>
</my:myFields>

它所做的是将模式作为文档加载,然后对于每个元素,在文档中查找该类型的元素,复制其属性,然后查找正确类型的 ref 并复制这些元素。

唯一的问题是它实际上并没有沿着模式寻找正确的部分,它只是通过名称来解决它们。对于小型模式,可能大多数时候它是正确的,但有时它可能会导致问题。但是对于您目前所展示的内容,它将起作用,并且是一个开始的地方

于 2012-07-16T13:15:50.677 回答
2

此 XSLT 2.0 样式表将探索您的模式,并为 minOccurs、maxOccurs 和类型插入适当的属性值。由于它是基于模块化的,因此很容易扩展您可能希望包含的其他 xsd 属性。为了便于演示,我在样式表中插入了模式。尽管在生产中,您可能会将其留在外部。所以相应地调整。

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38"
  xmlns:l="http://stackoverflow.com/questions/11502608"
  exclude-result-prefixes="xsl xs fn xsd l">
<xsl:output indent="yes"/>

 <xsd:schema targetNamespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-05-05T12:20:38"

 ... etc .. INSERT SCHEMA HERE !!!

 </xsd:schema>


<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()" />
 </xsl:copy>  
</xsl:template>

<xsl:function name="l:xsd-attrib" as="attribute()?">
 <xsl:param name="ele-name" as="xs:string"/>
 <xsl:param name="attri-name" as="xs:string"/>
 <xsl:sequence select="document('')/xsl:stylesheet/xsd:schema
    //(xsd:element[ (substring-after(@ref,'my:')=$ele-name) or (@name=$ele-name)]
    /@*[local-name(self::attribute())=$attri-name])" />
</xsl:function>

<xsl:template name="l:emit-xsd-attri">
 <xsl:param name="my-node" as="element()" />
 <xsl:param name="attri-name" as="xs:string" />  
  <xsl:variable name="ele" select="local-name( $my-node)" />
  <xsl:if test="l:xsd-attrib( $ele, $attri-name)">
   <xsl:attribute name="{$attri-name}"><xsl:value-of select="
     for $l in l:xsd-attrib( $ele, $attri-name) return
      if (contains( $l, ':')) then substring-after( $l, ':') else $l" />
    </xsl:attribute>
  </xsl:if>
</xsl:template> 

<xsl:template match="my:*"> 
 <xsl:copy>
  <xsl:apply-templates select="@*" />

  <xsl:call-template name="l:emit-xsd-attri">
   <xsl:with-param name="my-node" select="." />
   <xsl:with-param name="attri-name" select="'minOccurs'" />  
  </xsl:call-template>

  <xsl:call-template name="l:emit-xsd-attri">
   <xsl:with-param name="my-node" select="." />
   <xsl:with-param name="attri-name" select="'maxOccurs'" />  
  </xsl:call-template>

  <xsl:call-template name="l:emit-xsd-attri">
   <xsl:with-param name="my-node" select="." />
   <xsl:with-param name="attri-name" select="'type'" />  
  </xsl:call-template>

  <xsl:apply-templates select="node()" />
 </xsl:copy>  
</xsl:template>

</xsl:stylesheet>
于 2012-07-16T13:50:04.940 回答
0

当您根据模式验证 XML 实例文档时,结果是一个 PSVI(模式验证后实例),其中包含您需要的所有信息等等。唯一的问题是,如何在实践中掌握这些信息。这取决于您要使用的工具。例如,如果您使用 Xerces 作为模式验证器,则有一个 API 可以公开模式处理器添加到实例的 PSVI 注释。

于 2012-07-16T14:19:32.097 回答