2014-10-10 24 views
1

我有一個產生貨幣金額的XML,這通常是貨幣轉換的結果,因此需要以很高的準確度(在本例中爲4dp)顯示。我發現儘管我可以用列出的數字生成XML,但只要我在XML上強制執行模式,所有「不必要的」0數字都將被刪除。儘管我可以從計算機的角度理解這一點,但我們需要向我們的客戶準確地展示我們的數字精確度在所有數字中,無論每個數字的價值如何。對於他們的理解,如果我們保持一致,那麼我們總是在這些數字上顯示精確到小數點後4位,這一點更加清晰。這裏是你可以運行明白我的意思(比較之前和之後的結果)的例子:在十進制SimpleType中強制爲空的十進制佔位符

/* 
This script will create a schema collection, use it and drop it, so there is minimal chance of mess left behind 
*/ 
DECLARE @XSD XML 

SET @XSD = CONVERT(XML, 
'<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="xs3p.xsl"?> 
<xs:schema id="MyData" 
    targetNamespace="http://tempuri.org/MyData.xsd" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/MyData.xsd" 
    xmlns:mstns="http://tempuri.org/MyData.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:simpleType name="Money"> 
    <xs:restriction base="xs:decimal"> 
     <xs:totalDigits value="14" /> 
     <xs:fractionDigits value="4" /> 
    </xs:restriction> 
    </xs:simpleType> 
    <xs:element name="Root"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Amount" type="Money" minOccurs="1" maxOccurs="unbounded"> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema>') 

-- Clean if there is owt to clean 
IF EXISTS (SELECT * FROM sys.xml_schema_collections WHERE name = 'TestMoney') 
    DROP XML SCHEMA COLLECTION TestMoney 

-- use our xml bit above to make a new schema collection 
CREATE XML SCHEMA COLLECTION TestMoney AS @XSD 
GO 

-- we'll create our xml untyped first, so we can check it before we change it 
DECLARE @Untyped_XML XML 
-- this is the real test, with the schema enforced 
DECLARE @XML XML(TestMoney) 

-- build some test data 
SET @Untyped_XML = 
'<?xml version ="1.0"?> 
<Root xmlns ="http://tempuri.org/MyData.xsd"> 
    <Amount>1.0000</Amount> 
    <Amount>0.1234</Amount> 
</Root>' 

-- try to apply the schema collection to our xml, and show something useful whatever happens. 
BEGIN TRY 
    SET @XML = @Untyped_XML 
    SELECT @Untyped_XML AS Before, @XML AS [After] 
END TRY 
BEGIN CATCH 
    SELECT 
     ERROR_MESSAGE() AS Problem 
     ,@Untyped_XML  AS FailedXML 
END CATCH 
GO 
-- We don't really want this schema; it was just a test 
DROP XML SCHEMA COLLECTION TestMoney 

有什麼辦法來迫使數以全精度顯示?

+0

我以爲我有一段時間,使用模式匹配來確保小數點後四位是必需的,希望能強制結果以與模式匹配的方式顯示。唉,一個整數的規範形式沒有小數位,因此即使在單位值之後用'.0000'輸入時也不符合模式匹配。因此,向簡單類型添加''會導致錯誤。請參閱此[msdn文章](http://msdn.microsoft.com/en-us/library/bb510416.aspx)。我不想將所有的整數轉換爲字符串..! – 2014-10-10 18:45:24

+0

我已經開始思考,解決這個問題的方法可能是將樣式表應用於xml,使其看起來很漂亮(實際上並未更改這些值)。我認爲這一點,但我知之甚少;似乎編碼應該用於在SQL Server中應用xslt轉換......這超出了我的肯定。我正在使用SSIS包來保存xml文件,所以也許這將是應用轉換的地方?任何指針,如果這是正確的使用方法將非常歡迎!使用[this?](http://www.w3schools.com/xsl/el_decimal-format.asp) – 2014-10-12 18:22:54

回答

0

好的,所以似乎沒有簡單的方法來做到這一點 - 因此,我的解決方案是通過嘗試僅嘗試來驗證xml。然後,如果驗證沒有給出錯誤,我推斷untyped_XML可以通過鍵入併成功返回它。這有一個缺點,我將不得不繼續使用untyped xml,但這確實意味着至少它通過模式的度量來看起來很完美。要做到這一點,我更換了TRY/CATCH在這個問題:

-- try to apply the schema collection to our xml, and show something useful whatever happens. 
BEGIN TRY 
    SELECT @XML = @UNTYPED_XML 
END TRY 
BEGIN CATCH 
    SELECT @SchemaError = ERROR_MESSAGE() 
END CATCH 

-- If there was no error, then it is because the schema validated correctly, so show the original file. 
IF @SchemaError IS NULL 
    SELECT @Untyped_XML 
-- If there was an error, then display it, with the bad xml 
ELSE 
    SELECT @SchemaError AS 'ErrorEncountered', @Untyped_XML AS FailedXML 
GO 

這是不能令人滿意的,但它解決了我想這個問題,好像我會得到不應用樣式表的最好xml生成後(在SSIS中,可能使用XML任務)。

相關問題