2014-09-03 50 views
0

背景:我沒有太多的XML經驗。我有一個開箱即用的應用程序,它使用xml模式和模板。這個過程是這樣的:創建符合xsl和xslt文件中某些規則的XML文件?

  1. 我應該把正確的格式,並命名爲XML文件中的「下載/ Data」文件夾
  2. 程序檢測到「下載/數據」文件夾中的文件,讀取它,更新數據庫在xslt文件中使用代碼。 Xml文件被移入「下載/處理」文件夾。
  3. 如果原始xml文件格式不正確,則將其移入「下載/無效」文件夾。

問題:我需要創建xml文件,該文件將通過格式正確且將更新數據庫的驗證。 我現在的文件名是vattaxes.xml,看起來像這樣:

<?xml version="1.0"?> 
    <VATTax> 
     <ProcessingDateTime> 
    </ProcessingDateTime> 
    <AdditionalInfo> 
    </AdditionalInfo> 
    <Header> 
    <VERSION>5.0.1.7</VERSION> 
    <StoreId>1</StoreId> 
    <CreationDate>20140903</CreationDate> 
    <CreationTime>080636</CreationTime> 
    <CreatedOn>TPVIRTUAL-PC</CreatedOn> 
    <Provider>TP.net</Provider> 
    <Customer> 
    </Customer> 
    <Subject>StoreConfigBase</Subject> 
    <TruncateFields>0</TruncateFields> 
    </Header> 
      <createDate>20140903</createDate> 
      <createTime>082056</createTime> 
      <actionCode>2</actionCode> 
      <actionDescription>ADD</actionDescription>   
      <VATCode>5</VATCode> 
      <VATLongDescription>20% tax</VATLongDescription> 
      <VATShortDescription>20% tax</VATShortDescription> 
      <VATRate>20</VATRate> 
      <VATStartDate>2014-09-02</VATStartDate> 
      <VATEndDate>2015-09-03</VATEndDate> 
      <lastUser>1</lastUser> 
</VATTax> 

,檢查我的文件是否有效這個樣子的,被命名爲GV5_VAT.xsd XSD文件:

<?xml version="1.0"?> 
<xsd:schema xmlns="http://www.gold-solutions.com/GoldSchemas"  
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:gold="http://www.gold-solutions.com/GoldSchemas" 
     targetNamespace="http://www.gold-solutions.com/GoldSchemas" 
     elementFormDefault="qualified" attributeFormDefault="unqualified"> 
<xsd:annotation> 
    <xsd:documentation xml:lang="en"> 
    G.O.L.D. V5 TVAs schema V1.0 
    Copyright Aldata 2005 
    </xsd:documentation> 
</xsd:annotation> 

<xsd:element name="vattaxes"> 
    <xsd:complexType> 
     <xsd:sequence> 
      <xsd:element name="VATTax" type="VATTaxType" minOccurs="0" maxOccurs="unbounded"/> 
     </xsd:sequence> 
     <xsd:attribute name="processid" type="xsd:integer" use="required"/> 
    </xsd:complexType> 
</xsd:element> 

<xsd:complexType name="VATTaxType"> 
    <xsd:sequence> 
     <xsd:element name="header"> 
      <xsd:complexType> 
       <xsd:sequence> 
        <xsd:element name="createDate" type="xsd:date"/> 
        <xsd:element name="createTime" type="xsd:dateTime"/> 
        <xsd:element name="actionCode" type ="xsd:integer"/> 
        <xsd:element name="actionDescription" type="xsd:string"/> 
       </xsd:sequence> 
      </xsd:complexType> 
     </xsd:element> 
     <xsd:element name="VATCode" type="xsd:integer"/> 
     <xsd:element name="VATLongDescription" type="xsd:string"/> 
     <xsd:element name="VATShortDescription" type="xsd:string"/> 
     <xsd:element name="VATSystemCode" type="xsd:integer"/> 
     <xsd:element name="VATSystemDescription" type="xsd:string"/> 
     <xsd:element name="VATRate" type="xsd:decimal"/> 
     <xsd:element name="VATStartDate" type="xsd:date"/> 
     <xsd:element name="VATEndDate" type="xsd:date"/> 
     <xsd:element name="lastUser" type="xsd:string"/> 
    </xsd:sequence> 
</xsd:complexType> 
    </xsd:schema> 

我也有GV5_VAT.xsl

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:gold="http://www.gold-solutions.com/GoldSchemas" 
exclude-result-prefixes="gold"> 

<xsl:output method="xml" indent="yes" encoding="utf-8" /> 

<!-- Include the header --> 
<xsl:include  
    href="GV5_Base.xsl" /> 

<!-- 
Template: match the document (processing starts here) 
--> 
<xsl:template match="/"> 
    <xsl:apply-templates/> 
</xsl:template> 

<!-- 
Template: match the root element 
--> 
<xsl:template name="vattaxes" match="/gold:vattaxes" > 
    <UpdateDB> 
     <xsl:call-template name="BuildHeader" /> 
     <xsl:for-each select="./gold:VATTax"> 
      <xsl:call-template name="vattax" /> 
     </xsl:for-each> 
    </UpdateDB> 
</xsl:template> 

<!-- 
Template: match the single nodeRelation element 
--> 
<xsl:template name="vattax" > 
    <Transaction> 
     <!-- Not used elements : --> 
     <!-- 
      <xsd:element name="VATSystemCode" type="xsd:integer"/> 
      <xsd:element name="VATSystemDescription" type="xsd:string"/> 
      <xsd:element name="lastUser" type="xsd:string"/> 
     --> 

     <xsl:variable name="szPrintCode" select="./gold:VATShortDescription" /> 
     <xsl:variable name="szExternalID" select="./gold:VATCode" /> 
     <xsl:variable name="szName" select="./gold:VATLongDescription" /> 
     <xsl:variable name="dTaxPercent" select="./gold:VATRate" /> 
     <xsl:variable name="szEffectiveDate" select="concat(concat(substring (./gold:VATStartDate,1,4), substring(./gold:VATStartDate,6,2)), substring(./gold:VATStartDate,9,2))" /> 
     <xsl:variable name="szExpirationDate" select="concat(concat(substring(./gold:VATEndDate,1,4), substring(./gold:VATEndDate,6,2)), substring(./gold:VATEndDate,9,2))" /> 

     <!-- Operation selector --> 
     <xsl:variable name="actionCode" select="./gold:ACTIONCODE" /> 

     <xsl:choose> 
      <!-- from here changes for release 3.5 T.S. 05.02.2008 mer-X Software GmbH --> 

      <!-- Insert --> 
      <xsl:when test="$actionCode = '2'"> 

     *** sql inserts below*** 

,並提到GV5_Base.xsl

以上

這是要求他們所有的文件是相當簡單:

<?xml version="1.0" encoding="utf-8" ?> 
<PARAMETERS> 
<Conversion> 
    <!--<DownloadFolder>C:\siirto\tuotanto\pos\in</DownloadFolder>--> 
    <Assembly>not important</Assembly> 
    <Class>not important</Class> 
    <Mappings> 
    <Mapping name="VAT" > 
      <sourceschema validate="true">GV5_VAT.xsd</sourceschema> 
      <sourceidentifier>/*[local-name() = 'vattaxes' and namespace-uri() = 'http://www.gold-solutions.com/GoldSchemas']</sourceidentifier> 
      <xsltfile encoding="UTF-8">GV5_VAT.xsl</xsltfile> 
     </Mapping> 
    </Mappings> 
</Conversion> 
</PARAMETERS> 

所以,我應該怎麼名稱進入下載/ Data文件夾的.xml文件,它應該如何看裏面?

+0

尋求黃金解決方案的支持。你也可以從一個示例xml開始,請參閱http://stackoverflow.com/questions/17106/how-to-generate-sample-xml-documents-from-their-dtd-or-xsd – Kokkie 2014-09-03 07:51:25

+0

@Kokkie,非常感謝你很多,該文件正在驗證。它仍然沒有得到正確處理,但我認爲你已經指出了我的正確方向。 – jo1storm 2014-09-03 10:12:10

回答

1

好吧,我已經解決了我的問題。在希望我能幫助別人與同類之一,這裏是我的示例代碼,可以顯示你的問題的代碼做什麼:

文件稱他們所有:需要被讀取

<?xml version="1.0" encoding="utf-8" ?> 
<PARAMETERS> 
<Conversion> 
<!--<DownloadFolder>C:\siirto\tuotanto\pos\in</DownloadFolder>--> 
<Assembly>*****</Assembly> 
<Class>*****</Class> 
<Mappings> 
<Mapping name="Item01" > 
     <sourceschema validate="false">Items.xsd</sourceschema> 
     <sourceidentifier>/*[local-name() = 'Records']</sourceidentifier> 
     <xsltfile encoding="UTF-8">TrItems.xsl</xsltfile> 
    </Mapping> 
</Mappings> 
</Conversion> 
</PARAMETERS> 

XML文件(名字並不重要,我打電話給我products.xml,但它可能是banana.xml):

<?xml version="1.0" encoding="utf-8" ?> 
<Records> 
<Item> 
    <lStoreID>1</lStoreID> 
    <lItemID>1</lItemID> 
    <sActionType>Add</sActionType> 
</Item>  
</Records> 

Items.xsd並不重要(因爲驗證是假的),但應該是這樣的:

<?xml version="1.0"?> 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 

<xs:element name="Records"> 
<xs:complexType> 
<xs:sequence> 
<xs:element name="Item"> 
<xs:complexType> 
    <xs:sequence> 
     <xs:element name="lStoreID" type="xs:int"/> 
        <xs:element name="lItemID" type="xs:int"/> 
     <xs:element name="sActionType" type="xs:string"/> 
    </xs:sequence> 
</xs:complexType> 
</xs:element> 
</xs:sequence> 
</xs:complexType> 
</xs:element> 
</xs:schema> 

最後,TrItems。xsl文件看起來像這樣:

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:output encoding="utf-8" indent="yes" method="xml"/> 

<!-- Include the header --> 
<xsl:include href="GV5_Base.xsl" /> 
<!-- 
    Template: match the document (processing starts here) 
--> 
<xsl:template match="/"> 
    <xsl:apply-templates/> 
</xsl:template> 

<!-- 
    Template: match the root element 
    --> 
    <xsl:template match="Records"> 
    <UpdateDB> 
    <xsl:call-template name="BuildHeader" /> 
     <xsl:for-each select="Item"> 
    <xsl:call-template name="items" /> 
     </xsl:for-each> 
    </UpdateDB> 
    </xsl:template> 


<!-- 
Template: match the single Item element 
    --> 
    <xsl:template name="items" match="Item"> 
    <Transaction> 
     <xsl:variable name="StoreID" select="lStoreID" /> 
     <xsl:variable name="ItemID" select="lItemID" />    

     <!-- Operation selector --> 
     <xsl:variable name="ActionType" select="sActionType" /> 

     <xsl:choose> 
      <!-- Insert --> 
      <xsl:when test="$ActionType = 'Add'"> 
       <xsl:call-template name="InsItems"> 
        <xsl:with-param name="RStoreID" select="$StoreID" /> 
        <xsl:with-param name="PItemID" select="$ItemID" /> 
       </xsl:call-template> 
      </xsl:when>    
<!-- 
Template: insert into table Items 
--> 
<xsl:template name="InsItems"> 

    <xsl:param name="RStoreID" /> 
    <xsl:param name="PItemID" /> 
     <Insert> 
     <Table>Items</Table> 
     <Set>    
      <lRStoreID><xsl:value-of select="$RStoreID" /></lRStoreID> 
      <szItemID><xsl:value-of select="$PItemID" /></szItemID> 
     </Set> 
    </Insert> 
</xsl:template> 
    </xsl:stylesheet> 

GV5_Base顯示在我原來的帖子中。

上述代碼適用於包含兩列lRStoreID和szItemID的sql表項。我希望這能幫助有類似問題的人理解這些文件的作用。

1

你會得到一些關於你的日期和時間的錯誤;
元素createDate':'20140903'不是原子類型'xs:date'的有效值。
Element createTime':'080636'不是原子類型'xs:dateTime'的有效值。

在這裏你有一個示例XML,它確實驗證;

$ xmllint --schema GV5_VAT.xsd vattaxes.xml 
<?xml version="1.0"?> 
<gold:vattaxes xmlns:gold="http://www.gold-solutions.com/GoldSchemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" processid="0" xsi:schemaLocation="http://www.gold-solutions.com/GoldSchemas GV5_VAT.xsd"> 
     <gold:VATTax> 
       <gold:header> 
         <gold:createDate>2014-09-03</gold:createDate> 
         <gold:createTime>2014-09-03T08:06:36</gold:createTime> 
         <gold:actionCode>2</gold:actionCode> 
         <gold:actionDescription>ADD</gold:actionDescription> 
       </gold:header> 
       <gold:VATCode>5</gold:VATCode> 
       <gold:VATLongDescription>20% tax</gold:VATLongDescription> 
       <gold:VATShortDescription>20% tax</gold:VATShortDescription> 
       <gold:VATSystemCode>2</gold:VATSystemCode> 
       <gold:VATSystemDescription>sys descr</gold:VATSystemDescription> 
       <gold:VATRate>20</gold:VATRate> 
       <gold:VATStartDate>2014-09-02</gold:VATStartDate> 
       <gold:VATEndDate>2014-09-03</gold:VATEndDate> 
       <gold:lastUser>1</gold:lastUser> 
     </gold:VATTax> 
</gold:vattaxes> 
vattaxes.xml validates