2008年12月31日水曜日

XSLTをためす

こちら(XSLT - Transformation)(Transforming a Document With XSLT)を参考に.

  NSError *err=nil;

NSString *xmlPath= @"/path/to/cdcatalog.xml";
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:xmlPath]
options:0
error:&err];
if (err) {if (!xmlDoc) return;}

NSString *xslPath= @"/path/to/cdcatalog.xsl";;
NSXMLDocument *printDoc = (NSXMLDocument *)[xmlDoc
objectByApplyingXSLTAtURL:[NSURL fileURLWithPath:xslPath]
arguments:nil
error:&err];
if (err) {if (!printDoc) return;}

NSLog(@"%@", [printDoc XMLString]);


結果
cdcatalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>

cdcatalog.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

> <html><body><h2>My CD Collection</h2><table border="1"><tr bgcolor="#9acd32"><th align="left">Title</th><th align="left">Artist</th></tr><tr><td>Empire Burlesque</td><td>Bob Dylan</td></tr></table></body></html>


0 件のコメント: