note 45066 deleted from ref.xslt by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:35:27 +0000 |
| Subject: | note 45066 deleted from ref.xslt by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207374@lists.php.net to get a copy of this message | ||
Note Submitter: tom at kornack dot com
----
If you're wrangling with a plist in Mac OS X: here a bit of code to convert the plist into xml
using xslt and then accessing it using simplexml. This was modified from http://www.xmldatabases.org/WK/blog/1086?t=item
. Please do post improvements. This has been tested against iPhoto's AlbumData as well as
iTunes' data. Enjoy!
<?php
$xsl = new DomDocument();
$xsl->load("plistConvert.xsl");
$inputdom = new DomDocument();
$inputdom->load("iTunes Music Library.xml");
$proc = new XsltProcessor();
$proc->registerPhpFunctions();
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($inputdom);
$sxe = simplexml_import_dom($newdom);
print('<pre>');
print_r($sxe->dict);
print('</pre>');
?>
Here's the plistConvert.sxl file:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/plist">
<propertylist>
<xsl:apply-templates/>
</propertylist>
</xsl:template>
<xsl:template match="dict">
<dict>
<xsl:for-each select="key">
<xsl:variable name='n1' select='translate(., " ",
"_")'/>
<xsl:variable name='pName'>
<xsl:choose>
<!-- Need to do this because number tags like <0> are not valid?! -->
<xsl:when test='contains("0123456789", substring($n1, 1, 1))'>
<xsl:value-of select='concat("number_", $n1)'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$n1'/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$pName}">
<xsl:for-each select='following-sibling::*[1]'>
<xsl:choose>
<xsl:when test='name() = "dict"'>
<xsl:apply-templates select='.'/>
</xsl:when>
<xsl:when test='name() = "array"'>
<xsl:attribute name="array">collection</xsl:attribute>
<xsl:apply-templates/>
</xsl:when>
<xsl:when test='name() = "string"'>
<xsl:value-of select='.'/>
</xsl:when>
<xsl:when test='name() = "integer"'>
<!-- <xsl:attribute name="type">integer</xsl:attribute> -->
<xsl:value-of select='.'/>
</xsl:when>
<xsl:when test='name() = "date"'>
<!-- <xsl:attribute name="type">date</xsl:attribute> -->
<xsl:value-of select='.'/>
</xsl:when>
<xsl:when test='name() = "true"'>True</xsl:when>
<xsl:when test='name() = "false"'>False</xsl:when>
<xsl:when test='name() = "real"'>
<xsl:value-of select='.'/>
</xsl:when>
<xsl:when test='name() = "data"'>
<xsl:value-of select='.'/>
</xsl:when>
<xsl:otherwise>
<xsl:message>pListConverter: <xsl:value-of select='name()'/> not
implemented!
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</dict>
</xsl:template>
<!-- don't pass text thru -->
<xsl:template match="text()|@*">
</xsl:template>
</xsl:stylesheet>