fmdataweb Posted July 30, 2015 Posted July 30, 2015 I'm trying to import the following XML data: <?xml version="1.0" encoding="utf-8"?> <customers xmlns="http://acme.com/schema/v1_2/customers.xsd"> <customer ID="1"> <city>Monterey</city> <name>Herbson's Pices</name> <orders> <order ID="ORD2"> <num>1</num> <date>12-01-2002</date> <amount>23.54</amount> <items> <item> <productID>ABC123</productID> <quantity>1</quantity> <description>Oregano</description> <price>23.54</price> <extended>23.54</extended> </item> </items> </order> <order ID="ORD3"> <num>2</num> <date>01-06-2003</date> <amount>15.45</amount> <items> <item> <productID>23_45d</productID> <quantity>2</quantity> <description>Rosemary</description> <price>5.00</price> <extended>10.00</extended> </item> <item> <productID>t456</productID> <quantity>5</quantity> <description>Thyme</description> <price>1.09</price> <extended>5.45</extended> </item> </items> </order> </orders> </customer> <customer ID="2"> <city>New York</city> <name>A Pealing Desserts</name> <orders> <order ID="ORD4"> <num>1</num> <date>11-15-2002</date> <amount>115.00</amount> <items> <item> <productID>ABC123</productID> <quantity>5</quantity> <description>Lemon Zests</description> <price>23.00</price> <extended>115.00</extended> </item> </items> </order> </orders> </customer> </customers> I'm using the following XSLT: <?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output version="1.0" encoding="UTF-8" indent="no" method="xml"/> <xsl:template match="/"> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult"> <ERRORCODE>0</ERRORCODE> <PRODUCT BUILD="11/13/2002" NAME="FileMaker Pro" VERSION="6.0v4"/> <DATABASE DATEFORMAT="M/d/yyyy" LAYOUT="" NAME="Customers.FP5" RECORDS="" TIMEFORMAT="h:mm:ss a"/> <METADATA> <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ID" TYPE="NUMBER"/> <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Name" TYPE="TEXT"/> <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="City" TYPE="TEXT"/> </METADATA> <RESULTSET FOUND=""> <xsl:for-each select="./customers/customer"> <ROW MODID="" RECORDID=""> <COL> <DATA> <xsl:value-of select="@ID"/> </DATA> </COL> <COL> <DATA> <xsl:value-of select="./name"/> </DATA> </COL> <COL> <DATA> <xsl:value-of select="./city"/> </DATA> </COL> </ROW> </xsl:for-each> </RESULTSET> </FMPXMLRESULT> </xsl:template> </xsl:stylesheet> When I apply this stylesheet to the XML I'm not getting any data, i.e just this: <?xml version="1.0" encoding="utf-8"?> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult"> <ERRORCODE>0</ERRORCODE> <PRODUCT VERSION="6.0v4" NAME="FileMaker Pro" BUILD="11/13/2002"/> <DATABASE TIMEFORMAT="h:mm:ss a" RECORDS="" NAME="Customers.FP5" LAYOUT="" DATEFORMAT="M/d/yyyy"/> <METADATA> <FIELD TYPE="NUMBER" NAME="ID" MAXREPEAT="1" EMPTYOK="YES"/> <FIELD TYPE="TEXT" NAME="Name" MAXREPEAT="1" EMPTYOK="YES"/> <FIELD TYPE="TEXT" NAME="City" MAXREPEAT="1" EMPTYOK="YES"/> </METADATA> <RESULTSET FOUND=""/> </FMPXMLRESULT> I've noticed that if I change the 2nd line of the XML to just: <customers> it then transforms successfully and I can import it. I'd prefer not to have to manually change the XML - is there something I can do to the XSLT to allow it to work successfully. This is the result I'm expecting: <?xml version="1.0" encoding="utf-8"?> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult"> <ERRORCODE>0</ERRORCODE> <PRODUCT VERSION="6.0v4" NAME="FileMaker Pro" BUILD="11/13/2002"/> <DATABASE TIMEFORMAT="h:mm:ss a" RECORDS="" NAME="Customers.FP5" LAYOUT="" DATEFORMAT="M/d/yyyy"/> <METADATA> <FIELD TYPE="NUMBER" NAME="ID" MAXREPEAT="1" EMPTYOK="YES"/> <FIELD TYPE="TEXT" NAME="Name" MAXREPEAT="1" EMPTYOK="YES"/> <FIELD TYPE="TEXT" NAME="City" MAXREPEAT="1" EMPTYOK="YES"/> </METADATA> <RESULTSET FOUND=""> <ROW RECORDID="" MODID=""> <COL> <DATA>1</DATA> </COL> <COL> <DATA>Herbson's Pices</DATA> </COL> <COL> <DATA>Monterey</DATA> </COL> </ROW> <ROW RECORDID="" MODID=""> <COL> <DATA>2</DATA> </COL> <COL> <DATA>A Pealing Desserts</DATA> </COL> <COL> <DATA>New York</DATA> </COL> </ROW> </RESULTSET> </FMPXMLRESULT> Is there a way I can keep the raw XML as is but modify the XSL somehow to import successfully just as it does when I remove the: xmlns="http://acme.com/schema/v1_2/customers.xsd" string?
comment Posted July 30, 2015 Posted July 30, 2015 The xmlns="http://acme.com/schema/v1_2/customers.xsd" is called a namespace declaration. It places all elements of your input XML in the declared namespace. This is why your instruction: <xsl:for-each select="./customers/customer"> does not select anything. In order to address elements in a namespace, you must declare the same namespace in your stylesheet, assign it a prefix, and use that prefix when addressing the elements: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cx="http://acme.com/schema/v1_2/customers.xsd"> <xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/> <xsl:template match="/"> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult"> <ERRORCODE>0</ERRORCODE> <PRODUCT BUILD="11/13/2002" NAME="FileMaker Pro" VERSION="6.0v4"/> <DATABASE DATEFORMAT="M/d/yyyy" LAYOUT="" NAME="Customers.FP5" RECORDS="" TIMEFORMAT="h:mm:ss a"/> <METADATA> <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ID" TYPE="NUMBER"/> <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Name" TYPE="TEXT"/> <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="City" TYPE="TEXT"/> </METADATA> <RESULTSET FOUND=""> <xsl:for-each select="./cx:customers/cx:customer"> <ROW MODID="" RECORDID=""> <COL> <DATA> <xsl:value-of select="@ID"/> </DATA> </COL> <COL> <DATA> <xsl:value-of select="./cx:name"/> </DATA> </COL> <COL> <DATA> <xsl:value-of select="./cx:city"/> </DATA> </COL> </ROW> </xsl:for-each> </RESULTSET> </FMPXMLRESULT> </xsl:template> </xsl:stylesheet>
fmdataweb Posted July 30, 2015 Author Posted July 30, 2015 Thank you very much for that - I knew I had to reference the namespace I just wasn't sure of the syntax for doing so. Much appreciated and it's working well now.
beverly Posted July 31, 2015 Posted July 31, 2015 Yes, declare the namespace(s) in the source XML. Older versions of XSLT processors may need this as well:exclude-result-prefixes="<<space-delimited list>>" so append what Comment says: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cx="http://acme.com/schema/v1_2/customers.xsd"> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cx="http://acme.com/schema/v1_2/customers.xsd" exclude-result-prefixes="cx"> Use this IF you still get errors with your import. Generally the need 'rule' is if the FM result and the XML source have common elements such as ROW, COL, DATA, etc. SO, I just always use this attribute in the xsl:stylesheet element every time there are namespaces. Saves headaches. Keep in mind that a source XML may have more than one namespace. (ever look at Word or Excel as XML?) The other place to use exclude-result-prefixes is when you are EXPORTING from FileMaker and transforming for another namespace. In that case your prefix is 'fm' or 'fmp' or something unique. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fmp="http://www.filemaker.com/fmpxmlresult" exclude-result-prefixes="fmp"> beverly
Recommended Posts
This topic is 3669 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now