0

我正在将 a 转换plist为 XML,我很困惑为什么我for-each会返回两个响应,而如果我更改for-each为 a value-of,我根本不会得到任何回报。其他一切正常,我只想从我的 XSLT中获取单个Description// TitleUsername

XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

  <dict>

    <key>Playlists</key>
    <array>
      <dict>
        <key>All Items</key>
        <true/>
        <key>Playlist Items</key>
        <array>
          <dict>
            <key>Track ID</key>
            <integer>0</integer>
          </dict>
          <dict>
            <key>Track ID</key>
            <integer>1</integer>
          </dict>
          <dict>
            <key>Track ID</key>
            <integer>2</integer>
          </dict>
        </array>
        <key>Playlist Persistent ID</key>
        <string>pID505050</string>
      </dict>
    </array>

    <key>Tracks</key>
    <dict>

      <key>0</key>
      <dict>
        <key>Album</key>
        <string>Funeral</string>
        <key>Artist</key>
        <string>The Arcade Fire</string>
        <key>Name</key>
        <string>Neighborhood #3 (Power Out)</string>
        <key>Track ID</key>
        <string>0</string>
      </dict>

      <key>1</key>
      <dict>
        <key>Album</key>
        <string>Untrue</string>
        <key>Artist</key>
        <string>Burial</string>
        <key>Name</key>
        <string>Archangel</string>
        <key>Track ID</key>
        <string>1</string>
      </dict>

    </dict>

    <key>Upload Information</key>
    <dict>
      <key>Playlist Description</key>
      <string>My description</string>
      <key>Playlist Title</key>
      <string>Mytitle</string>
      <key>Username</key>
      <string>Maven</string>
    </dict>

  </dict>

</plist>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <playlist>

      <xsl:for-each select="/*/*/dict[1]/dict">
        <xsl:element name="Artist">
          <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Artist']" />
        </xsl:element>
        <xsl:element name="Album">
          <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Album']" />
        </xsl:element>
        <xsl:element name="TrackID">
          <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Track ID']" />
        </xsl:element>
      </xsl:for-each>

      <xsl:for-each select="/*/*/dict">
        <xsl:element name="Description">
          <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Playlist Description']" />
        </xsl:element>
        <xsl:element name="Title">
          <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Playlist Title']" />
        </xsl:element>
        <xsl:element name="Username">
          <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Username']" />
        </xsl:element>

      </xsl:for-each>

    </playlist>

  </xsl:template>   

</xsl:stylesheet>

当前 XSL 输出:

<?xml version="1.0" encoding="UTF-8"?>
<playlist>
  <Artist>The Arcade Fire</Artist>
  <Album>Funeral</Album>
  <TrackID>0</TrackID>
  <Artist>Burial</Artist>
  <Album>Untrue</Album>
  <TrackID>1</TrackID>
  <Description></Description>
  <Title></Title>
  <Username></Username>
  <Description>My description</Description>
  <Title>Mytitle</Title>
  <Username>Maven</Username>
</playlist>

提前致谢!

4

2 回答 2

2

我假设一些事情,因为你的问题不太清楚:

  • 示例 XML 和 XSLT 格式不正确,因此我关闭了根元素
  • 您想要所有曲目的列表,而不仅仅是第一个,然后是播放列表上传信息
  • 我冒昧地将每个音轨包装在输出 XML 的一个元素中。

然后,我使您的 for-each XPath 选择更加明确:

<xsl:for-each select="//dict[preceding-sibling::*[1]='Tracks']/dict">

<xsl:for-each select="//dict[preceding-sibling::*[1]='Upload Information']">

同样,我为 value-of 清理 XPath,例如:

<xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Artist']" />

请注意使用 local-name() 来检查第一个前面的兄弟节点实际上是否称为“键”。您的输入数据不是很好的 XML :)

最后,我清理了元素创建等。这是 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <playlist>
        <xsl:for-each select="//dict[preceding-sibling::*[1]='Tracks']/dict">
            <Track>
                <Artist>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Artist']" />
                </Artist>
                <Album>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Album']" />
                </Album>
                <TrackID>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Track ID']" />
                </TrackID>
            </Track>
        </xsl:for-each>

        <xsl:for-each select="//dict[preceding-sibling::*[1]='Upload Information']">
            <Description>
                <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Playlist Description']" />
            </Description>
            <Title>
                <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Playlist Title']" />
            </Title>
            <Username>
                <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Username']" />
            </Username>
        </xsl:for-each>
    </playlist>
</xsl:template>    

这是生成的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<playlist>
   <Track>
      <Artist>The Arcade Fire</Artist>
      <Album>Funeral</Album>
      <TrackID>0</TrackID>
   </Track>
   <Track>
      <Artist>Burial</Artist>
      <Album>Untrue</Album>
      <TrackID>1</TrackID>
   </Track>
   <Description>My description</Description>
   <Title>Mytitle</Title>
   <Username>Maven</Username>
</playlist>
于 2013-06-06T15:07:46.000 回答
0

您可能对这种使用模板而不是for-each元素的替代转换感兴趣。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <playlist>
      <xsl:apply-templates select="plist/dict/dict[preceding-sibling::key = 'Tracks']/dict" mode="track"/>
      <xsl:apply-templates select="plist/dict/dict[preceding-sibling::key = 'Upload Information']" mode="info"/>
    </playlist>
  </xsl:template>

  <xsl:template match="dict" mode="track">
    <track>
      <Artist>
        <xsl:value-of select="string[preceding-sibling::key = 'Artist']"/>
      </Artist>
      <Album>
        <xsl:value-of select="string[preceding-sibling::key = 'Album']"/>
      </Album>
      <TrackID>
        <xsl:value-of select="string[preceding-sibling::key = 'Track ID']"/>
      </TrackID>
    </track>
  </xsl:template>

  <xsl:template match="dict" mode="info">
    <info>
      <Description>
        <xsl:value-of select="string[preceding-sibling::key = 'Playlist Description']"/>
      </Description>
      <Title>
        <xsl:value-of select="string[preceding-sibling::key = 'Playlist Title']"/>
      </Title>
      <Username>
        <xsl:value-of select="string[preceding-sibling::key = 'Username']"/>
      </Username>
    </info>
  </xsl:template>

</xsl:stylesheet>

输出

<?xml version="1.0" encoding="utf-8"?>
<playlist>
   <track>
      <Artist>The Arcade Fire</Artist>
      <Album>Funeral</Album>
      <TrackID>0</TrackID>
   </track>
   <track>
      <Artist>Burial</Artist>
      <Album>Untrue</Album>
      <TrackID>1</TrackID>
   </track>
   <info>
      <Description>My description</Description>
      <Title>Mytitle</Title>
      <Username>Maven</Username>
   </info>
</playlist>
于 2013-06-06T15:56:57.040 回答