我正在阅读一本电子书,它有以下xml代码.
<?xml version="1.0"?>
<?xml-stylesheet href="sonnet.xsl" type="text/xsl"?>
<?cocoon-process type="xslt"?>
<!DOCTYPE sonnet [
<!ELEMENT sonnet (auth:author, title, lines)>
<!ATTLIST sonnet public-domain CDATA "yes"
type (Shakespearean | Petrarchan) "Shakespearean">
<!ELEMENT auth:author (last-name,first-name,nationality,
year-of-birth?,year-of-death?)>
<!ELEMENT last-name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT nationality (#PCDATA)>
<!ELEMENT year-of-birth (#PCDATA)>
<!ELEMENT year-of-death (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT lines (line,line,line,line,
line,line,line,line,line,line,line,
line,line,line)>
<!ELEMENT line (#PCDATA)>
]>
<!-- Default sonnet type is Shakespearean, the other allowable -->
<!-- type is "Petrarchan." -->
<sonnet type="Shakespearean">
<auth:author xmlns:auth="http://www.authors.com/">
<last-name>Shakespeare</last-name>
<first-name>William</first-name>
<nationality>British</nationality>
<year-of-birth>1564</year-of-birth> …Run Code Online (Sandbox Code Playgroud) 好吧,我有这个xml
<roots>
<root>
<name>first</name>
<item type='test'><something>A</something></item>
<item type='test'><something>B</something></item>
<item type='test'><something>C</something></item>
<item type='test'><something>A</something></item>
<item type='other'><something>A</something></item>
<item type='test'><something>B</something></item>
<item type='other'><something>D</something></item>
</root>
<root>
<name>second</name>
<item type='test'><something>E</something></item>
<item type='test'><something>B</something></item>
<item type='test'><something>F</something></item>
<item type='test'><something>A</something></item>
<item type='other'><something>A</something></item>
<item type='test'><something>B</something></item>
<item type='other'><something>D</something></item>
</root>
</roots>
Run Code Online (Sandbox Code Playgroud)
现在我需要获得每个根节点的唯一值到目前为止我有
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="text"/>
<xsl:key name="item-by-value" match="something" use="."/>
<xsl:key name="rootkey" match="root" use="name"/>
<xsl:template match="/">
<xsl:for-each select="key('rootkey','second')">
<xsl:for-each select="item/something">
<xsl:if test="generate-id() = generate-id(key('item-by-value', normalize-space(.)))">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
如果我使用"First"作为获得第一根的关键我得到一个好的结果ABCD
如果我使用"秒"我怎么只得到EF但我需要结果是ABDFE