1

質問があります:

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>

どうすればこれを達成できますか?

4

1 に答える 1

0

XSL変換を使用して、SQLServerの種類を目的のXSDの種類に戻すことができます。

変換の適用方法は、アプリケーションによって異なります。テーブルの静的スキーマを作成している場合は、VisualStudioやmsxslなどを使用できます。これがサーバーからの定期的な要求である場合は、XSL変換(SQLXMLマネージドクラス)を適用する方が適している場合があります。

追加のタイプで構築できるスタイルシートは次のとおりです。

<?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に変更します。

于 2013-07-06T15:43:30.353 に答える