Tuesday, August 2. 2011
Transform XML using PHP DOMDocument ... Posted by gcd5137
at
12:59
Comments (0) Trackbacks (0) Not modified
Transform XML using PHP DOMDocument and XslProcessorThe sample code below can to transform XML using XSL using PHP. First, set up the XSL processor and stylesheet. $xp = new XsltProcessor(); Next, load the XML file to be transformed.
Lastly, transform the XML and display the result.
Thursday, April 28. 2011
Transform RSS XML into plist for iOS Posted by gcd5137
at
12:25
Comments (0) Trackbacks (0) Last modified on 2011-04-28 12:58
Transform RSS XML into plist for iOSI was working on an iPhone app project and found it to be much easier to load a .plist file than to go through the trouble of downloading and parsing an RSS feed. Luckily RSS is XML and so is a .plist file so XSLT to the rescue. Let's walk through the XSL. As usual, check the namespaces in the input and add them to your XSL. In this case, I was pulling from the New York Times which was full of them. <xsl:stylesheet version="1.0" exclude-result-prefixes="pheedo nyt media atom dc" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:nyt="http://www.nytimes.com/namespaces/rss/2.0 xmlns:pheedo="http://www.pheedo.com/namespace/pheedo">
Next, set some of the important output options. <xsl:output method="xml" indent="yes" doctype-public="-//Apple//DTD PLIST 1.0//EN" doctype-system="http://www.apple.com/DTDs/PropertyList-1.0.dtd" encoding="ISO-8859-1" /> The encoding attribute prevents some unwanted character conversions which will prevent XCode from loading it. I'm still not sure the best way to handle the character encoding. The root template will output the plist wrapper. I mocked up the plist first in XCode to get an idea for what the resulting XML should look like. <xsl:template match="/"> <plist version="1.0"> <array> <xsl:apply-templates select="/rss/channel/item" /> </array> </plist> </xsl:template> Now create the item template. This is not the most robust XSL but it works for the purposes of demonstrating the concept. I also deleted some additional output elements for the sake of brevity. <xsl:template match="item"> <dict> <key>title</key> <string> <xsl:value-of select="title" /> </string> <key>link</key> <string> <xsl:value-of select="link" /> </string> <key>image</key> <string> <xsl:value-of select="image" /> </string> </dict> </xsl:template> Run that through your xsl processor and you should see output similar to below. <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>title</key> <string>Seattle Journal: Long After Microsoft, Allen and Gates Cast Shadows Over City</string> <key>link</key> <string>http://feeds.nytimes.com/click.phdo?i=5b4fa1b8abefd39cd4eaa299314d0a29</string> <key>image</key> <string /> </dict> ...more content here </array> </plist>
Tuesday, April 6. 2010
Getting antlib to work - looping ... Posted by gcd5137
at
16:08
Comments (0) Trackbacks (0) Not modified
Getting antlib to work - looping through files in a directoryI was working with Apache Ant and came across antlib, which offers some cool extensions and seemed to be the right tool for what I needed to do. Here's how I got it to work. First, I had to update my version of Ant to 1.8 (or later). Next, I added the namespace to my ant build XML file <project> element. xmlns:antcontrib="antlib:net.sf.antcontrib"> Then, I declared the extension using typedef. <typedef resource="net/sf/antcontrib/antlib.xml" I ended up using the namespaced approach because it worked on the first try. In this case, I was looping through a directory and getting information about the files. <antcontrib:foreach param="file" target="file-info"> The target "file-info" gets called for each file specified in the path. See ant and antlib docs for more info. Monday, March 29. 2010
Configuring PHP Proxy with JSON web ... Posted by gcd5137
in PHP at
10:04
Comments (0) Trackbacks (0) Last modified on 2010-03-29 10:57
Configuring PHP Proxy with JSON web serviceI was working on an AJAX web application and got stuck on a problem trying to get my php proxy to use the Digg web service to provide results in json format. After reading the Digg API docs, I ended up solving the problem using curl instead of file_get_contents, as you might with HTML or XML. See curl setopt below, more specifically CURLOPT_HTTPHEADER and CURLOPT_USERAGENT. $curl_handle = curl_init();
This solved the problem and now works as expected with Firefox, Chrome, and IE. Friday, March 5. 2010
Generating UUIDs with XSL and Java Posted by gcd5137
in Java, XSLT at
15:19
Comments (3) Trackbacks (0) Last modified on 2010-03-29 10:51
Generating UUIDs with XSL and JavaSample code showing how to generate unique IDs using XSL and Java. I am running Saxon 9 and Java 1.6. First, set up the xmlns:uuid extension at the stylesheet level. <xsl:stylesheet version="2.0" Later on, create a random unique ID using the uuid extension. <xsl:variable name="uid" select="uuid:randomUUID()"/> Output the value of the unique ID. <xsl:value-of select="$uid"/> I like to set up an attribute set to reuse. <xsl:attribute-set name="uid"> And then reference it when creating the result. <some-element xsl:use-attribute-sets="uid"/> Tuesday, July 14. 2009
Exporting XML from Oracle using Java ... Posted by gcd5137
in Java at
14:10
Comments (0) Trackbacks (0) Last modified on 2010-03-29 10:52
Exporting XML from Oracle using Java ResultSetI have done this with PHP before, but needed to do it again using similar logic with Java and XSL. The purpose was to extract information from an Oracle database. This is the meat of the Java function body, which loops through a SQL query ResultSet object and returns an XML document (minus the exception handling). I then write that XML Doc to an output file. The 'cols' attribute was just for debugging.
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); Friday, May 1. 2009
Transforming XML to SQL using XSL Posted by gcd5137
at
16:53
Comments (0) Trackbacks (0) Last modified on 2009-05-01 17:02
Transforming XML to SQL using XSLI recently came across a project that required me to take a bunch of XML and put it into MySQL, a relational database system. I thought about the easiest way to migrate the data without too much manual effort. I launched jEdit, my current XSL development tool of choice, and started writing some templates. First, I had to set the output to text and handle whitespace.
I set up some templates to match my input XML elements and spit out the appropriate SQL statements. <xsl:template match="topics/record"> The resulting output is a SQL script I can use to automatically load the data into MySQL. I used phpMyAdmin to manage the database tables and import the SQL output from my XSL transform. This worked like a charm and saved me hours of time! Tuesday, April 7. 2009
Using DOMXPath with PHP Posted by gcd5137
in PHP at
09:09
Comments (0) Trackbacks (0) Last modified on 2010-03-29 10:51
Using DOMXPath with PHPI stumbled upon some very helpful information on php.net yesterday while working with PHP, XML, and DOM. The DOMXpath class makes working with XML way easier, especially when you need more complex features than SimpleXml. The function below accepts a DOMDocument and XPath string as input parameters and returns a DOMNodeList, which you can easily iterate over almost as if it were an array. public static function getNodes($dom, $xpathString){ Here's a simple example. You need to make up some XML and adjust the XPath as necessary. $file = "data.xml"; Tuesday, March 10. 2009
Using Catalogs with Saxon Posted by gcd5137
in XSLT at
23:10
Comments (0) Trackbacks (0) Last modified on 2010-03-29 10:53
Using Catalogs with SaxonHere's an XSL tip that I use quite a bit and is very handy. If you are working with XML that uses DTDs or Schemas, you most likely need to resolve the SYSTEM or PUBLIC identifiers that are used for any required resources. Most Java XSL processors, like Saxon, allow you to use the apache commons resolver to add support for Catalogs and resolve those identifiers. java -cp "saxon.jar;resolver.jar;" net.sf.saxon.Transform -x org.apache.xml.resolver.tools.ResolvingXMLReader -y org.apache.xml.resolver.tools.ResolvingXMLReader -r org.apache.xml.resolver.tools.CatalogResolver -o result.out input.xml style.xsl Your CatalogManager.properties should also be on the classpath and might look something like this. catalogs=catalog.xml;C:/somepath/catalog And your XML catalog might look like this. <?xml version="1.0"?> Sunday, March 8. 2009
Keep your content out of attributes Posted by gcd5137
at
21:00
Comments (0) Trackbacks (0) Not modified
Keep your content out of attributesWhen creating an XML DTD or Schema, a developer often needs to decide when to use an element or attribute. These types of decisions can impact the project success. Here are a few questions that might help you make the decision.
An example of why such decisions are important is shown when trying to work with XML that contains translated text in attribute values. The XML spec does not allow reserved characters (i.e. &) or entities (i.e. é) in attribute values, so prepare for all kinds of problems. Parsers will complain, stylesheet processors will fail, publishing engines will throw exceptions, and lots of time will be spent trying to resolve the problem. There are many more questions to ask, but these should help make the decision easier. |
Calendar
QuicksearchArchivesCategoriesSyndicate This BlogBlog Administration |
|||||||||||||||||||||||||||||||||||||||||||||||||
