【问题标题】:How to change the format of XML schema I get from SQL server如何更改我从 SQL Server 获得的 XML 模式的格式
【发布时间】:2012-08-11 02:49:15
【问题描述】:

我有一个问题:

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>

我怎样才能做到这一点?

【问题讨论】:

    标签: sql-server-2008 xsd for-xml


    【解决方案1】:

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

    如何应用转换取决于您的应用程序。如果要为表创建静态架构,则可以使用 Visual Studio 或 msxsl 之类的东西。如果这是来自服务器的例行请求,那么 Applying an XSL Transformation (SQLXML Managed Classes) 可能更合适。

    您可以使用其他类型构建的样式表是:

    <?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>
    

    如果需要查看在转换过程中被剥离的属性,请将开头的Strip参数更改为false。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多