我需要从 itunes library.xml 文件中提取轨道 ID 和位置。 我找到了一些 XSLT 解决方案,但它们都是基于 XSLT 2.0 版本的。
我仅限于 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>
最佳答案
因此,对于 Tracks 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> </xsl:text>
</xsl:template>
</xsl:stylesheet>
如果 plist 始终将 Location 作为最后一个条目,那么您可以简单地说
<xsl:value-of select="following-sibling::dict[1]/string[last()]" />
但是通过找到正确的键值然后获取其第一个后续字符串
来实现这一点会更稳健。
关于xml - itunes 库上的 XSLT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19767922/