Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Importing XML, no parent record id in child table
Message
General information
Forum:
Visual FoxPro
Category:
XML, XSD
Miscellaneous
Thread ID:
01042677
Message ID:
01073046
Views:
35
>Hi Aleksey,
>
>I have finally got round to applying this suggestion to our live XML file. Problem is the Child.ParentID field is .null., any ideas why that might be. I guess it is the 'parent::/' which is not getting evaluated as expected but can't find any way of looking at what is going on. Is there any way of debuging the parent::/ line to see if it is working correctly?
>
>Our XML has six tables within it.
>
>Thx,
> Nick

Hi Nick,

David is almost right about [parent::foo/f1]. For XMLField object, XPath expression is executed in context of XMLTable node ("BAR" in this case) and it is passed to XML DOM as is. The meaning of the expression is to get an immediate parent node named "foo" and find its immediate child element named "f1". You should refer to MSXML4 XPath reference for complete XPath syntax.

It is likely that simple name replacement doesn't work for your case because XML document uses a namespace. In this case, node names should be properly qualified using namespace prefix (when XMLNameIsXPath=.F., XMLAdapter automatically qualifies XMLName). XMLAdapter automatically declares "dataset" prefix for XMLAdapter.XMLNamespace, in addition, you can declare custom prefix using XMLAdapter.SelectionNamespaces property.

Here is an example:
SET MEMOWIDTH TO 150

* use XPath scripting to get FOREIGN key from the parent

CLOSE DATABASES all
CLEAR 

LOCAL oXA as XMLAdapter, oBar as XMLTable

oXA=CREATEOBJECT("XMLAdapter")

TEXT TO cXML NOSHOW
<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>
<VFPDataSet xmlns="MyNameSpace">
	<xsd:schema id="VFPDataSet"  targetNamespace="MyNameSpace" xmlns="MyNameSpace"
			xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
		<xsd:element name="VFPDataSet" msdata:IsDataSet="true">
			<xsd:complexType>
				<xsd:choice maxOccurs="unbounded">
					<xsd:element name="foo" minOccurs="0" maxOccurs="unbounded">
						<xsd:complexType>
							<xsd:sequence>
								<xsd:element name="f1" type="xsd:int"/>
								<xsd:element name="BAR" minOccurs="0" maxOccurs="unbounded">
									<xsd:complexType>
										<xsd:sequence>
											<xsd:element name="f2">
												<xsd:simpleType>
													<xsd:restriction base="xsd:string">
														<xsd:maxLength value="5"/>
													</xsd:restriction>
												</xsd:simpleType>
											</xsd:element>
										</xsd:sequence>
									</xsd:complexType>
								</xsd:element>
							</xsd:sequence>
						</xsd:complexType>
					</xsd:element>
				</xsd:choice>
				<xsd:anyAttribute namespace="http://www.w3.org/XML/1998/namespace" processContents="lax"/>
			</xsd:complexType>
		</xsd:element>
	</xsd:schema>
	<foo>
		<f1>1</f1>
		<BAR>
			<f2>1</f2>
		</BAR>
		<BAR>
			<f2>11</f2>
		</BAR>
		<BAR>
			<f2>111</f2>
		</BAR>
		<BAR>
			<f2>1111</f2>
		</BAR>
		<BAR>
			<f2>11111</f2>
		</BAR>
	</foo>
	<foo>
		<f1>2</f1>
		<BAR>
			<f2>2</f2>
		</BAR>
		<BAR>
			<f2>22</f2>
		</BAR>
		<BAR>
			<f2>222</f2>
		</BAR>
	</foo>
	<foo>
		<f1>3</f1>
		<BAR>
			<f2>333</f2>
		</BAR>
		<BAR>
			<f2>3333</f2>
		</BAR>
		<BAR>
			<f2>33333</f2>
		</BAR>
	</foo>
</VFPDataSet>
ENDTEXT

oXA.LoadXML(cXML,.F.)

oBar=oXA.Tables(STRCONV("BAR",5))

AddXmlField(oBar,"parent::node()/dataset:f1",.F.,"f1_1","I",-1,.T.)
AddXmlField(oBar,"parent::dataset:foo/dataset:f1",.F.,"f1_2","I",-1,.T.)

oXA.SelectionNamespaces = STRCONV("xmlns:myprefix='MyNameSpace'",5) 
AddXmlField(oBar,"parent::myprefix:foo/myprefix:f1",.F.,"f1_3","I",-1,.T.)


AdapterToCursors(oXA)


FUNCTION AdapterToCursors(oXA as XMLAdapter)
	FOR EACH oXT IN oXA.Tables
		oXT.ToCursor()
		SELECT (oXT.Alias)
		? ALIAS()
		LIST
	NEXT
endfunc

FUNCTION AddXmlField(oXT,cXMLName,lAttribute,cAlias,cDataType,nMaxLength,lXPath)
	oXF=CREATEOBJECT("XMLField")
	IF VERSION(5)>=900
		oXF.XMLNameIsXPath=lXPath
	ENDIF 
	oXF.XMLName=STRCONV(cXMLName,5)
	oXF.IsAttribute= lAttribute
	oXF.Alias=cAlias
	oXF.DataType=cDataType
	oXF.MaxLength=nMaxLength
	oXF.IsNULL=.T.
	oXT.Fields.Add(oXF,oXF.XMLName)
	RETURN oXF	
ENDFUNC
Thanks,
Aleksey.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform