合并XML和XSD文件

时间:2012-07-16 10:38:01

标签: .net xml xslt xsd

我有以下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转换?

3 个答案:

答案 0 :(得分: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并复制这些元素

唯一的问题是它没有实际沿着架构寻找正确的部分,它只是通过名称来解决它们。对于小型模式,可能大部分时间都是正确的,但偶尔也可能会导致问题。但是,对于你现在所展示的内容它将起作用,它是一个从

开始的地方

答案 1 :(得分: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>

答案 2 :(得分:0)

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