我正在编写xsl样式表以从iTunes Music Library中提取信息. xml文件. 我想在一个数组中存储播放列表的轨道信息,然后迭代它们以获取更多信息.我很困惑如何在xslt中的数组中存储值? 我的尝试
          我想在一个数组中存储播放列表的轨道信息,然后迭代它们以获取更多信息.我很困惑如何在xslt中的数组中存储值?
我的尝试在这里:
<?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" /> 
    <xsl:template match="/"> 
    <xsl:variable name="tracks"
           select="plist/dict/array/dict[integer[preceding-sibling::key[1]='Playlist ID']=6711]/array/dict/integer[preceding-sibling::key[1]='Track ID']" />
    <!-- I want to iterate over that array outside for-each loop and gather more information, The below code is not working.-->
   <xsl:for-each select="$tracks">
  <xsl:value-of select="." />
  <xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/integer[preceding-sibling::key[1]='Track ID']" />
  <xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/string[preceding-sibling::key[1]='Name']" />
  <xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/string[preceding-sibling::key[1]='Total Time']" />
  <xsl:value-of select="plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']="."]/string[preceding-sibling::key[1]='Location']" />
  <xsl:text>
</xsl:text>
</xsl:for-each>
    </xsl:template>
    </xsl:stylesheet> 
 在数组变量“轨道”中填充的典型轨道ID在iTunes列表中以下面的方式表示.我想列出存储在数组中的每个Track Ids的Name,Location,time infor.我的情况有问题.
<plist>
  <dict>
    <dict>
      <dict>
        <key>Track ID</key>
        <integer>1633</integer>
        <key>Name</key>
        <string>Right here</string>
        <key>Kind</key>
        <string>MPEG audio file</string>
        <key>Total Time</key>
        <integer>358870</integer>
        <key>Location</key>
        <string>/Users/rakesh/Music/iTunes/iTunes%20Media/Music/track1633.mp3</string>
      </dict>
      <dict> 
        <!-- Next Track info -->
      </dict>
    </dict>
  </dict>
</plist> 
 我被困在这里.任何XSLT专家都可以帮助我吗?
您可以创建一个变量并使用XPath表达式一次性填充其结果,而不是通过构建它来进行交互.<xsl:variable name="tracks" select="plist/dict/array/dict[integer[preceding-sibling::key[1]='Playlist ID']=6711]/array/dict/integer[preceding-sibling::key[1]='Track ID']" />
或者你可以在你的文档的开头使用xsl:keys,我假设轨道信息的路径是/ plist / dict / array / dict,第一个dict键是Tracks
<xsl:key 
        name="playlists"
        match="plist/dict/array/dict/array/dict/integer[preceding-sibling::key[1]='Track ID']"
        use="../../../integer[preceding-sibling::key[1]='Playlist ID']"
/>
<xsl:key 
       name="tracks" 
       match="/plist/dict/array[preceding-sibling::key[1]='Tracks']/dict"
       use="integer[preceding-sibling::key[1]='Track ID']"
/> 
 这允许您执行键(‘播放列表’,’4555′)以返回与播放列表ID 4555相关联的所有轨迹以及键(‘轨迹’,’1234′)以获取与轨道ID 1234相关联的dict对象
然后你可以将两者结合起来做
<xsl:variable "mytracks" select="key('tracks',key('playlists','6711'))" /> 
 这将设置$mytracks等于播放列表6711中的曲目的dict对象数组.它还具有xsl:key授予的速度增强的好处
编辑更新—-
我猜你正试图从这里制作一个CSV,所以这段代码应该这样做
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="text" />
  <xsl:key
    name="playlists"
    match="plist/dict/array/dict/array/dict/integer[preceding-sibling::key[1]='Track ID']"
    use="../../../integer[preceding-sibling::key[1]='Playlist ID']"
  />
  <xsl:key
    name="tracks"
    match="/plist/dict/dict/dict"
    use="integer[preceding-sibling::key[1]='Track ID']"
  />
  <xsl:template match="/">
    <xsl:variable name="myplaylist" select="'6711'"/>
    <xsl:for-each select="key('tracks',key('playlists',$myplaylist))">
      <xsl:value-of select="integer[preceding-sibling::key[1]='Track ID']"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="string[preceding-sibling::key[1]='Name']"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="integer[preceding-sibling::key[1]='Total Time']"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="string[preceding-sibling::key[1]='Location']"/>
      <xsl:text>
</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet> 
 匹配不同的播放列表ID只需更改myplaylist的值
– 不带xsl:key的EDED版本,再次只是改变myplaylist变量的值
–EDIT现在mofified为播放列表的原始排序顺序
–EDIT尝试解决Qt限制
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="text" />
  <xsl:template match="/">
    <xsl:variable name="myplaylist" select="'4053'"/>
    <xsl:variable name="playlist_tracks" select="/plist/dict/array/dict[integer[preceding-sibling::key[1]='Playlist ID']=$myplaylist]/array/dict/integer[preceding-sibling::key[1]='Track ID']" />
    <xsl:for-each select="$playlist_tracks">
      <xsl:variable select="." name="current" />
      <xsl:for-each select ="/plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']=$current]" >
        <xsl:value-of select="integer[preceding-sibling::key[1]='Track ID']"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="string[preceding-sibling::key[1]='Name']"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="integer[preceding-sibling::key[1]='Total Time']"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="string[preceding-sibling::key[1]='Location']"/>
        <xsl:text>
</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
        
             