[英]XSD to SQL Server table
我有幾個定義報告的 XSD 文件,想知道是否有辦法將其加載到我可以查詢的表中。
元素 XSD 定義了必填字段,如下所示:
<xs:simpleType name="NHSNumberStatusIndicatorCode_Withheld_Type">
<xs:restriction base="ns:AlphaNumeric_Type">
<xs:length value="2"/>
<xs:enumeration value="01"/>
<xs:enumeration value="02"/>
<xs:enumeration value="03"/>
<xs:enumeration value="04"/>
<xs:enumeration value="05"/>
<xs:enumeration value="06"/>
<xs:enumeration value="07"/>
<xs:enumeration value="08"/>
</xs:restriction>
</xs:simpleType>
我需要將其轉換為
Field_Name Type Length enumeration MaxLen MinLen
NHSNumber... AlphaNumeric_Type 2 01
NHSNumber... AlphaNumeric_Type 2 02
NHSNumber... AlphaNumeric_Type 2 03
等等
有些字段沒有枚舉,它們可能看起來像:
<xs:simpleType name="CDSOrganisationIdentifier_Type">
<xs:restriction base="ns:AlphaNumeric_Type">
<xs:minLength value="3"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
或者
<xs:simpleType name="Number_Type">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{10}"/>
</xs:restriction>
</xs:simpleType>
您可以將其加載到 XML 變量中,然后使用各種 SQL 服務器 XML 方法來 select 感興趣的節點並提取值。 有關詳細信息,請參閱xml 數據類型方法和路徑表達式 (XQuery) 。
在下文中,我將您的 XSD 片段包裝在一個“架構”元素中,該元素還定義了“xs:”命名空間前綴。
DECLARE @xml XML = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="NHSNumberStatusIndicatorCode_Withheld_Type">
<xs:restriction base="ns:AlphaNumeric_Type">
<xs:length value="2"/>
<xs:enumeration value="01"/>
<xs:enumeration value="02"/>
<xs:enumeration value="03"/>
<xs:enumeration value="04"/>
<xs:enumeration value="05"/>
<xs:enumeration value="06"/>
<xs:enumeration value="07"/>
<xs:enumeration value="08"/>
</xs:restriction>
</xs:simpleType>
</xs:schema >'
SELECT
N.node.value('(../../@name)[1]', 'nvarchar(max)') AS Field_Name,
N.node.value('(../@base)[1]', 'nvarchar(max)') AS Type,
N.node.value('(../xs:length/@value)[1]', 'int') AS Length,
N.node.value('@value[1]', 'nvarchar(max)') AS enumeration
FROM @xml.nodes('//xs:enumeration') N(node)
.nodes()
方法從提供的 xml 中選擇所有枚舉節點。前導//
選擇任意深度的節點。 N(nodes)
為所選結果提供任意表和列別名。 .value()
方法然后用於 select 在長度的情況下來自所選節點、父節點或兄弟節點的屬性值。 名稱上的@
前綴指的是屬性而不是子節點。 因為許多 XPath 選擇器可能有多個 select 值,所以您經常需要使用[1]
或(...)[1]
到 select 只是第一個。
結果:
字段名稱 | 類型 | 長度 | 枚舉 |
---|---|---|---|
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 01 |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 02 |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 03 |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 04 |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 05 |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 06 |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 07 |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 08 |
看到這個 db<>fiddle 。
XSD 也可以加載到表的 XSD 類型列中,並通過將@xml
替換為列引用以與上述類似的方式訪問。
以上是針對此特定枚舉類型場景進行編碼的。 如果您的實際 XSD 包含不遵循此模式的其他類型定義,您可能需要單獨處理這些情況。
更新:為了適應您修改后的帖子中的以下其他情況......
<xs:simpleType name="CDSOrganisationIdentifier_Type">
<xs:restriction base="ns:AlphaNumeric_Type">
<xs:minLength value="3"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Number_Type">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{10}"/>
</xs:restriction>
</xs:simpleType>
需要采取不同的方法。 在初始.nodes()
調用以獲取<xs:simpleType...>
節點后,將進行其他調用以訪問選定的從屬節點,其中一些可能存在也可能不存在。 最后,更新了 select 列表中的.value()
調用,以提取與幾個選定節點之一相關的數據。
SELECT
S.Node.value('(@name)[1]', 'nvarchar(max)') AS Field_Name,
R.Node.value('(@base)[1]', 'nvarchar(max)') AS Type,
R.Node.value('(xs:length/@value)[1]', 'int') AS Length,
E.Node.value('@value[1]', 'nvarchar(max)') AS enumeration,
R.Node.value('(xs:maxLength/@value)[1]', 'int') AS MaxLength,
R.Node.value('(xs:minLength/@value)[1]', 'int') AS MinLength,
R.Node.value('(xs:pattern/@value)[1]', 'nvarchar(max)') AS Pattern
FROM @xml.nodes('//xs:simpleType') S(Node)
OUTER APPLY S.Node.nodes('xs:restriction') R(Node) -- Likely always one occurrence
OUTER APPLY R.Node.nodes('xs:enumeration') E(Node) -- Zero or more occurrences
OUTER APPLY
類似於子選擇的LEFT JOIN
,只是沒有使用ON
條件。
結果是:
字段名稱 | 類型 | 長度 | 枚舉 | 最長長度 | 最小長度 | 圖案 |
---|---|---|---|---|---|---|
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 01 | null | null | null |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 02 | null | null | null |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 03 | null | null | null |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 04 | null | null | null |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 05 | null | null | null |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 06 | null | null | null |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 07 | null | null | null |
NHSNumberStatusIndicatorCode_Withheld_Type | ns:AlphaNumeric_Type | 2個 | 08 | null | null | null |
CDSOrganisationIdentifier_Type | ns:AlphaNumeric_Type | null | null | 5個 | 3個 | null |
號碼_類型 | xs:字符串 | null | null | null | null | [0-9]{10} |
請參閱此更新的 db<>fiddle 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.