我正在将 a 转换plist
为 XML,我很困惑为什么我for-each
会返回两个响应,而如果我更改for-each
为 a value-of
,我根本不会得到任何回报。其他一切正常,我只想从我的 XSLT中获取单个Description
// Title
。Username
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>
提前致谢!