2013-11-04 15 views
2

我需要從itunes library.xml文件中提取音軌ID和位置。 我發現了一些XSLT解決方案,但它們都是基於XSLT 2.0版的。itunes上的XSLT庫

我僅限於XSLT版本1.0。

任何人都可以幫助如何做到這一點。

輸出應該是:

98,location--- 
100,location 2 

的幫助 馬蒂亞斯

<?xml version="1.0" encoding="UTF-8"?> 
<plist version="1.0"> 
    <dict> 
     <key>Tracks</key> 
     <dict> 
     <key>98</key> 
     <dict> 
      <key>Track ID</key> 
      <integer>98</integer> 
      <key>Name</key> 
      <string>xxxxxx</string> 
      <key>Location</key> 
      <string>location---</string> 
     </dict> 
     <key>100</key> 
     <dict> 
      <key>Track ID</key> 
      <integer>100</integer> 
      <key>Name</key> 
      <string>name2</string> 
      <key>Location</key> 
      <string>location 2</string> 
     </dict> 
     </dict> 
    </dict> 
</plist> 
+0

您是否嘗試編寫任何代碼?您是否嘗試將XSLT2.0轉換爲1.0並查看發生了什麼 - 許多XSLT2.0命令向後兼容? – 2013-11-04 23:06:31

回答

0

更改文件頭到1.0 XSLT版本號非常感謝。我無法想象這個簡單的輸出需要任何不支持1.0的東西。

2

因此,對於軌道dict中的每個key,您要提取Location。這個怎麼樣:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="text" /> 

    <xsl:template match="/"> 
    <xsl:apply-templates select="plist/dict/dict/key" /> 
    </xsl:template> 

    <xsl:template match="key"> 
    <xsl:value-of select="." /> 
    <xsl:text>,</xsl:text> 
    <!-- find the dict corresponding to this key, and extract the value of 
     the Location entry --> 
    <xsl:value-of select=" 
     following-sibling::dict[1]/key[. = 'Location']/following-sibling::string[1]" /> 
    <xsl:text>&#10;</xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

如果plist中始終把位置作爲最後一項,那麼你可以簡單地說

<xsl:value-of select="following-sibling::dict[1]/string[last()]" /> 

但找到正確的鍵值做,然後採取了先上後下string是更強大。

0

假設輸入正確(還有一個關閉</dict>),您可以使用以下樣式表。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="text" /> 
    <xsl:template match="/"> 
     <xsl:apply-templates /> 
    </xsl:template> 
    <xsl:template match="plist"> 
     <xsl:apply-templates /> 
    </xsl:template> 
    <xsl:template match="dict[parent::plist]"> 
     <xsl:apply-templates /> 
    </xsl:template> 
    <xsl:template match="key[.='Tracks']/dict"> 
     <xsl:for-each select="descendant::dict"> 
     <xsl:value-of select="preceding-sibling::key" /> 
     <xsl:text>,</xsl:text> 
     <xsl:value-of select="descendant::key[.='Location']/following-sibling::string" /> 
     <xsl:text /> 
     </xsl:for-each> 
    </xsl:template> 
    <xsl:template match="key|string[preceding-sibling::key[1]='Name']" /> 
</xsl:stylesheet> 

編輯 @Ian是的,你是對的,當然。我改變了我的評論。

請注意,您必須依靠導航文檔樹, following-sibling由於XML文件的淺層次結構。

+0

XML可能很難看,但它是標準的XML「序列化」格式的序列化,用於遍佈Mac OS X和iOS的數百個不同目的。在精神上它更像JSON - 一個'dict'是從字符串鍵到可以是字符串,布爾值,數字,另一個'dict'或上述任何數組的值的映射。有時plist使用XML序列化存儲,有時它們以二進制格式存儲。 –