0

Background: I don't have much experience with XML. I've got an out of the box application which works with xml schemas and templates. The process is this:

  1. I should put properly formatted and named xml file in the "Download/Data" folder
  2. Program detects the file in the "Download/Data" folder, reads it, updates the database using code in xslt files. Xml file is moved into "Download/Processed" folder.
  3. If the original xml file is not formatted properly, it is moved into "Download/Invalid" folder.

Problem: I need to create xml file that will pass the validation which is properly formatted and will update the database. My current file name is vattaxes.xml and looks like this:

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

The xsd file that checks whether my file is valid looks like this and is named 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>

I also have 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***

And the above mentioned 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>

The file which is calling them all is fairly straightforward:

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

So, how should I name the .xml file that goes into the Downloads/Data folder and how should it it look inside?

jo1storm
  • 125
  • 9
  • Ask for support from gold-solutions. You could also start with a sample xml, see http://stackoverflow.com/questions/17106/how-to-generate-sample-xml-documents-from-their-dtd-or-xsd – Kokkie Sep 03 '14 at 07:51
  • @Kokkie, thank you very much, the file is being validated. It still doesn't get processed correctly but I think you've pointed me in the right direction. – jo1storm Sep 03 '14 at 10:12

2 Answers2

2

You will get a few errors regarding your date and time;
Element createDate': '20140903' is not a valid value of the atomic type 'xs:date'.
Element createTime': '080636' is not a valid value of the atomic type 'xs:dateTime'.

Here you have a sample XML which does validates;

$ 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
Kokkie
  • 546
  • 6
  • 15
1

Ok, I have solved my problem. In hope I can help someone else with the similar one, here's my sample code that can show you what the question code does:

File calling them all:

<?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 file that needs to be read(name is not important, I called mine products.xml but it could have been banana.xml):

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

Items.xsd is not important (because validation is false) but should look like this:

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

Finally, TrItems.xsl file looks like this:

  <?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 is shown in my original post.

The above code works for sql table Items with two columns, lRStoreID and szItemID. I hope this will help someone with similar problem understand what these files do.

jo1storm
  • 125
  • 9