我需要从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>发布于 2013-11-04 20:51:21
因此,对于曲目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()]" />但是,通过找到正确的键值,然后获取它的第一个后续string会更健壮。
发布于 2013-11-04 20:43:41
将文件头中的XSLT版本号更改为1.0。我无法想象如此简单的输出需要任何1.0不支持的东西。
发布于 2013-11-04 21:05:55
假设输入正确(又有一个结束的</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来导航文档树。
https://stackoverflow.com/questions/19767922
复制相似问题