2008-11-11 41 views
1

我通過轉換遺留系統的專有數據模型來定期創建XSD模式。這工作很好。但是,遺留系統僅允許我指定參數的非常基本的屬性,例如數據類型(int,string等)。使用XSLT元數據驅動生成

我想通過一種機制來增強XSL轉換,該機制允許我添加元數據以提供更多轉換細節。我想到了像Java屬性符號這樣的將屬性分配給XPath的方法。

想象一下下面的例子:

遺留系統的數據模型(實際上是整齊的,但最適合於演示目的)

<datamodel> 
    <customer> 
    <firstName type="string"/> 
    <lastName type="string"/> 
    <age type="int"> 
    <customer> 
</datamodel> 

元數據

customer/firstName/@nillable=false 
customer/lastName/@nillable=false 
customer/age/@nillable=true 
customer/firstName/@minOccurs=1 
customer/firstName/@maxOccurs=1 
customer/lastName/@minOccurs=1 
customer/lastName/@maxOccurs=1 

產生XSD架構

... 
<xs:complexType name="customerType"> 
    <xs:sequence> 
    <xs:element name="firstName" type="xs:string" nillable="false" minOccurs="1" maxOccurs="1"/> 
    <xs:element name="lastName" type="xs:string" nillable="false" minOccurs="1" maxOccurs="1"/> 
    <xs:element name="age" type="xs:int" nillable="true"/> 
    </xs:sequence> 
</xs:complexType> 
... 

您對此有何看法?有沒有辦法將元數據包含到XSL樣式表中?

回答

2

最好的解決方案是通過添加缺失的元數據來修改遺留數據。

修改後的「數據模型」詞彙表的一個實例可能是這樣的:

<datamodel xmlns:nm="my:new.meta"> 
    <customer> 
     <firstName type="string" 
        nm:nillable="false" 
        nm:minOccurs="1" 
        nm:maxOccurs="1" 
        /> 
     <lastName type="string" 
        nm:nillable="false" 
        nm:minOccurs="1" 
        nm:maxOccurs="1" 
        /> 
     <age type="int" nm:nillable="true"/> 
    </customer> 
</datamodel> 

把新的屬性在不同的命名空間是很容易地從已經支持的屬性區分它們的好方法。通常不建議在名稱空間中使用屬性,因此如果要避免這種情況,可以使用子元素(屬於新名稱空間)而不是屬性。使新屬性屬於不同的名稱空間可能會導致遺留模式驗證不會拒絕它們。

如果由於某些原因無法修改遺留數據,我建議不要在XSLT樣式表中包含新屬性(這是完全可能的,例如將其定義爲全局內容<xsl:variable>),而是將這些新屬性作爲單獨的XML文件或一組一個或多個XML文件提供。

使用XSLT document()函數在XSLT轉換期間可以動態訪問任何XML文件。具有新屬性的XML文件的實例可能如下所示:

<newProperties xmlns:nm="my:new.meta"> 
    <customer> 
     <firstName nm:nillable="false" 
        nm:minOccurs="1" 
        nm:maxOccurs="1" 
        /> 
     <lastName nm:nillable="false" 
        nm:minOccurs="1" 
        nm:maxOccurs="1" 
        /> 
     <age nm:nillable="true"/> 
    </customer> 
</newProperties> 

希望這對我有所幫助。

乾杯,

Dimitre Novatchev

1

「你怎麼看?」

兩個 三件事。

  1. 修復遺留元數據。它是XML。添加東西給它。如果必須添加一個名稱空間。

  2. 如果您無法修復遺留元數據,那麼誰將保留第二組元數據不是XML表示法?誰會做元數據更改的雙重入口?通過任何人的可能性是什麼?

  3. 考慮使用XML作爲附加元數據,而不是僞XPath。一致性將有助於那些在你弄清楚發生了什麼事情之後。

+0

1)行,是誠實的:傳統系統是不與數據模型生成代碼擺弄而不是整齊如上所述的任何機會閉源。但是,這足以用於演示目的。 2)我意識到維護方面的缺點 – poezn 2008-11-11 13:00:38