Sql server 2008 如何更改从SQLServer获取的XML模式的格式

Sql server 2008 如何更改从SQLServer获取的XML模式的格式,sql-server-2008,xsd,for-xml,Sql Server 2008,Xsd,For Xml,我有一个问题: SELECT top 0 * FROM sometable FOR XML AUTO, ELEMENTS, XMLSCHEMA ('MyURI') 此查询返回一个架构: <xsd:element name="ClientName"> <xsd:simpleType> <xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompare

我有一个问题:

SELECT top 0 * FROM sometable FOR XML AUTO, ELEMENTS, XMLSCHEMA ('MyURI')
此查询返回一个架构:

<xsd:element name="ClientName">
  <xsd:simpleType>
    <xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
      <xsd:maxLength value="50" /> 
    </xsd:restriction>
  </xsd:simpleType>
</xsd:element>

但我想要更像这样的东西:

<xsd:element name="ClientName">
  <xsd:simpleType>
   <xsd:restriction base="xsd:string">
     <xsd:maxLength value="50" /> 
   </xsd:restriction>
  </xsd:simpleType>
</xsd:element>


如何实现这一点?

您可以使用XSL转换将SQL Server类型更改回所需的XSD类型

如何应用转换取决于您的应用程序。如果要为表创建静态模式,那么可以使用VisualStudio或msxsl之类的工具。如果这是来自服务器的例行请求,则可能更适合

可以使用其他类型构建的样式表包括:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
  xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes"
  xmlns="http://www.w3.org/1999/XSL/Transform"
  >
  <xsl:output method="xml" indent="yes"/>
  <xsl:param name="Strip">false</xsl:param>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@base">
    <xsl:attribute name="{name()}">
      <xsl:choose>
        <xsl:when test=".='sqltypes:nvarchar'">xsd:string</xsl:when>
        <!-- Add additional tests here -->
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@sqltypes:*">
    <xsl:if test="$Strip=true">
      <xsl:comment>
        <xsl:text>Stripped (</xsl:text>
        <xsl:value-of select="concat(name(), '=&quot;', ., '&quot;')"/>
        <xsl:text>)</xsl:text>
      </xsl:comment>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

假的
xsd:string
剥去(
)
如果需要查看在转换过程中剥离的属性,请将开头的剥离参数更改为false