创建适合xsl和xslt文件中某些规则的XML文件?

时间:2014-09-03 07:25:57

标签: xml xslt xml-parsing xsd

背景:我对XML没有多少经验。我有一个开箱即用的应用程序,它可以使用xml架构和模板。过程是这样的:

  1. 我应该将正确格式化和命名的xml文件放在"下载/数据"夹
  2. 程序检测"下载/数据"中的文件。文件夹,读取它,使用xslt文件中的代码更新数据库。 Xml文件被移入"下载/处理"文件夹中。
  3. 如果原始xml文件格式不正确,则会将其移至"下载/无效"文件夹中。
  4. 问题:我需要创建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>
    

    检查我的文件是否有效的xsd文件如下所示,名为GV5_VAT.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" ?>
     <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:gold="http://www.gold-solutions.com/GoldSchemas"
    >
    
    <xsl:output method="xml" indent="yes" encoding="utf-8" />
    
    <xsl:variable name="parHeaderCreationDate" />
    <xsl:variable name="parHeaderCreationTime" />
    <xsl:variable name="parHeaderCreatedOn" />
    <xsl:variable name="parHeaderSubject" />
    
    
    <!--
        Template: build header
      -->
    <xsl:template name="BuildHeader">
        <Header>
            <Version>3.5.0.0</Version>
            <CreationDate><xsl:value-of select="$parHeaderCreationDate"/></CreationDate>
            <CreationTime><xsl:value-of select="$parHeaderCreationTime"/></CreationTime>
            <CreatedOn><xsl:value-of select="$parHeaderCreatedOn"/></CreatedOn>
            <Provider>Aldata Gold</Provider>
            <iDocType>positems</iDocType>
            <Customer>WN</Customer>
            <Subject><xsl:value-of select="$parHeaderSubject"/></Subject>
            <TruncateFields>1</TruncateFields>
        </Header>
    </xsl:template>
       </xsl:stylesheet>
    

    调用它们的文件非常简单:

    <?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>
    

    那么,我应该如何命名进入Downloads / Data文件夹的.xml文件以及它应该如何显示?

2 个答案:

答案 0 :(得分:1)

您的日期和时间会有一些错误;
元素createDate&#39;:&#39; 20140903&#39;不是原子类型的有效值&#39; xs:date&#39;。
元素createTime&#39;:&#39; 080636&#39;不是原子类型的有效值&#39; xs:dateTime&#39;。

这里有一个示例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

答案 1 :(得分: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显示在我的原帖中。

上面的代码适用于带有两列的sql表Items,lRStoreID和szItemID。我希望这可以帮助有类似问题的人了解这些文件的作用。