Message
From
09/05/2017 08:29:22
 
General information
Forum:
Visual FoxPro
Category:
XML, XSD
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2008 R2
Network:
Windows Server 2008 R2
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01650938
Message ID:
01650958
Views:
125
Likes (1)
>>>Hi all,
>>>
>>>I'm trying to read in an xml file that is a transaction so have a header and detail lines in the one xml file. The xmltocursor command reads in the header details fine but the stock lines are dumped into a memo that I cannot parse. I've played with the parameters to no avail. Is there is way to separate out the stock lines from the file?
>>>
>>>~M
>>
>>Yes. Can you post the XML document schema here? Or, on its absence, an example?
>The file is below, the utf as 16 is incorrect and should be 8.

There were two details - the OrderID and the ItemTypeNumber - that made this a bit different than I thought of at first. If it wasn't for those, a simple selectNodes on the original XML file would suffice to fetch the details.
LOCAL OriginalFile AS String

TEXT TO m.OriginalFile NOSHOW
<?xml version="1.0" encoding="utf-16"?>
<Orders>
	<Order xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
		<DeliveryCompanyName />
		<DeliveryTitle>Mr</DeliveryTitle>
		<DeliveryFirstName>First</DeliveryFirstName>
		<DeliveryLastName>Name</DeliveryLastName>
		<DeliveryAddress1>64 Zoo Lane</DeliveryAddress1>
		<DeliveryAddress2></DeliveryAddress2>
		<DeliveryAddress3 />
		<DeliveryTown></DeliveryTown>
		<DeliveryCounty />
		<DeliveryCountry>244</DeliveryCountry>
		<DeliveryPhone>1234567</DeliveryPhone>
		<BillingTitle>Mr</BillingTitle>
		<BillingFirstName>First</BillingFirstName>
		<BillingLastName>Name</BillingLastName>
		<BillingCompanyName>Technology Company</BillingCompanyName>
		<BillingAddress1>64 Zoo Lane</BillingAddress1>
		<BillingAddress2 />
		<BillingAddress3 />
		<BillingTown></BillingTown>
		<BillingCounty></BillingCounty>
		<BillingPostcode>postcode</BillingPostcode>
		<BillingCountry>244</BillingCountry>
		<BillingPhone>1234567</BillingPhone>
		<CompanyCode />
		<Currency>3</Currency>
		<DomainID>2</DomainID>
		<OrderID>1033</OrderID>
		<OrderDate>2017-02-23T13:59:57.343</OrderDate>
		<Login>login@company.com</Login>
		<TotalGross>405.90</TotalGross>
		<TotalNet>330.00</TotalNet>
		<TotalTax>75.90</TotalTax>
		<OrderLines>
			<OrderLine ItemTypeName="OrderDiscount">
				<OrderLineID>87</OrderLineID>
				<Code>MHA - Fixed Amount off an Order</Code>
				<Name>MHA - Fixed Amount off an Order</Name>
				<Quantity>1</Quantity>
				<UnitPrice>0.000000</UnitPrice>
				<UnitPriceDiscounted>-20.000000</UnitPriceDiscounted>
				<UnitDiscount>20.000000</UnitDiscount>
				<LinePrice>0.00</LinePrice>
				<LinePriceDiscounted>-20.00</LinePriceDiscounted>
				<TotalTax>-4.60</TotalTax>
				<TotalNet>-20.00</TotalNet>
				<TotalGross>-24.60</TotalGross>
			</OrderLine>
			<OrderLine ItemTypeName="DeliveryOption">
				<OrderLineID>89</OrderLineID>
				<Code>ZeusFleet</Code>
				<Name>Delivery Via Fleet</Name>
				<Quantity>1</Quantity>
				<UnitPrice>0.000000</UnitPrice>
				<UnitPriceDiscounted>0.000000</UnitPriceDiscounted>
				<UnitDiscount>0.000000</UnitDiscount>
				<LinePrice>0.00</LinePrice>
				<LinePriceDiscounted>0.00</LinePriceDiscounted>
				<TotalTax>0.00</TotalTax>
				<TotalNet>0.00</TotalNet>
				<TotalGross>0.00</TotalGross>
			</OrderLine>
			<OrderLine ItemTypeName="Product">
				<OrderLineID>88</OrderLineID>
				<Code>BB=CURVE9320-WH</Code>
				<Name>BlackBerry Curve 9320 White</Name>
				<Quantity>1</Quantity>
				<UnitPrice>350.000000</UnitPrice>
				<UnitPriceDiscounted>350.000000</UnitPriceDiscounted>
				<UnitDiscount>0.000000</UnitDiscount>
				<LinePrice>350.00</LinePrice>
				<LinePriceDiscounted>350.00</LinePriceDiscounted>
				<TotalTax>80.50</TotalTax>
				<TotalNet>350.00</TotalNet>
				<TotalGross>430.50</TotalGross>
			</OrderLine>
		</OrderLines>
		<OrderDiscounts>
			<OrderDiscount>
				<Code>MHA - Fixed Amount off an Order</Code>
				<Name>MHA - Fixed Amount off an Order</Name>
				<TotalTax>0.0000</TotalTax>
				<TotalNet>20.0000</TotalNet>
				<TotalGross>20.0000</TotalGross>
				<UnitDiscount />
				<UnitPriceDiscounted />
				<LineDiscount />
				<LinePriceDiscounted />
			</OrderDiscount>
		</OrderDiscounts>
	</Order>
</Orders>
ENDTEXT

* this will do for the top level
XMLTOCURSOR(m.OriginalFile, "Orders")

BROWSE

* to fetch the orders details, we'll have to transform into another XML document
* this will fetch also, as columns, the order ID that applies to the detail, and
* the Item Type Number (whatever that may be)
LOCAL GetOrderDetailsStylesheet AS String

TEXT TO m.GetOrderDetailsStylesheet NOSHOW
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  version="1.0" exclude-result-prefixes="xsd xsi">
  
  <xsl:output method="xml"/>

  <xsl:template match="/">
    <xsl:element name="OrderLines">
      <xsl:apply-templates select="Orders/Order"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="Order">
    <xsl:variable name="orderID" select="OrderID"/>
    <xsl:for-each select="OrderLines/OrderLine">
      <xsl:element name="OrderLine">
        <xsl:element name="OrderID">
          <xsl:value-of select="$orderID"/>
        </xsl:element>
        <xsl:element name="ItemTypeName">
          <xsl:value-of select="@ItemTypeName"/>
        </xsl:element>
        <xsl:copy-of select="*"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
ENDTEXT

LOCAL XML AS MSXML2.DOMDocument60
LOCAL XSLT AS MSXML2.DOMDocument60

m.XML = CREATEOBJECT("MSXML2.DOMDocument.6.0")
m.XML.async = .F.
m.XML.loadXML(m.OriginalFile)

m.XSLT = CREATEOBJECT("MSXML2.DOMDocument.6.0")
m.XSLT.async = .F.
m.XSLT.loadXML(m.GetOrderDetailsStylesheet)

XMLTOCURSOR(m.XML.transformNode(m.XSLT), "OrderLines")

SELECT OrderLines

BROWSE
----------------------------------
António Tavares Lopes
Previous
Next
Reply
Map
View