1

是否可以从 SQL Server 2005 返回嵌套的 XMLSCHEMA?

现在我有一个存储过程来生成一个可以执行以返回 XML 文档的 NESTED 选择查询。我通过将其附加到每个嵌套查询的末尾来做到这一点:

FOR XML AUTO   ,TYPE  ,ELEMENTS

我只是将其更改为:

FOR XML AUTO   ,TYPE  ,ELEMENTS, XMLSCHEMA

我可能能够返回一个嵌套的 XSD 文档。

返回的内容看起来像一个 XSD,其中粘贴了另一个完整的 XSD,这意味着每个 XSD 都有自己的命名空间和 schemaLocation。我只需要它们在根 XSD 中显示为 xsd:elements。

SELECT 
   Stuff,
   (SELECT OtherStuff 
    FROM test2  
    FOR XML AUTO, TYPE, ELEMENTS, XMLSCHEMA) 
FROM test 
FOR XML AUTO, TYPE, ELEMENTS, XMLSCHEMA

这是返回的内容:

<xsd:schema xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet26" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet26" elementFormDefault="qualified">
  <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
  <xsd:element name="test" type="sqltypes:xml" />
</xsd:schema>
<test xmlns="urn:schemas-microsoft-com:sql:SqlRowSet26">
  <Stuff>Stuff</Stuff>
  <xsd:schema xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet25" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" xmlns="" targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet25" elementFormDefault="qualified">
    <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
    <xsd:element name="test2">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="OtherStuff" minOccurs="0">
            <xsd:simpleType>
              <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                <xsd:maxLength value="50" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <test2 xmlns="urn:schemas-microsoft-com:sql:SqlRowSet25">
    <OtherStuff>OtherStuff</OtherStuff>
  </test2>
</test>

这是我需要返回的:

<xsd:schema xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet26" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet26" elementFormDefault="qualified">
    <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
    <xsd:element name="test" type="sqltypes:xml" />
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="Stuff" minOccurs="0">
                <xsd:simpleType>
                    <xsd:restriction base="sqltypes:varchar">
                        <xsd:maxLength value="50"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="test2">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="OtherStuff" minOccurs="0">
                            <xsd:simpleType>
                                <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                                    <xsd:maxLength value="50" />
                                </xsd:restriction>
                            </xsd:simpleType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

SQL 返回的实际上是语法错误的 XML。不应该有两个 ROOT 元素

任何帮助将不胜感激。

4

0 回答 0