我有这种xml
<?xml version="1.0" encoding="UTF-8"?>
<setting>
<Key name="com.ahsay.afc.cpf.UserGroup" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="1328200856753" />
<Value name="rsv-group-name" inheritParentAttribute="Y" type="string" data="group 1" />
<Value name="rsv-user-type" inheritParentAttribute="Y" type="string" data="backup-user" />
<Value name="rsv-owner" inheritParentAttribute="Y" type="string" data="" />
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name1" />
</Key>
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name2" />
</Key>
</Key>
<Key name="com.ahsay.afc.cpf.UserGroup" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="1328200856753" />
<Value name="rsv-group-name" inheritParentAttribute="Y" type="string" data="group 2" />
<Value name="rsv-user-type" inheritParentAttribute="Y" type="string" data="backup-user" />
<Value name="rsv-owner" inheritParentAttribute="Y" type="string" data="" />
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name3" />
</Key>
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name4" />
</Key>
</Key>
</setting>
我想从rsv-login-name data =“value”获取rsv-group-name data =“value”
即我给解析器name3,我希望它返回组2
最好我到现在为止是这样的:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="umm" match="Value" use="@data"/>
<xsl:key name="amm" match="Value" use="@name"/>
<xsl:template match="/">
<xsl:for-each select="key('umm','name3')">
<p>
<xsl:value-of select="../../@name" />
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Witch给我“com.ahsay.afc.cpf.UserGroup”并不是最好的结果:)
答案 0 :(得分:0)
试试这个:
<xsl:value-of select="../../Value[@name = 'rsv-group-name']/@data"/>
答案 1 :(得分:0)
使用密钥提高效率的解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kValueByData" match="Value" use="@data"/>
<xsl:key name="kValueByKeyAndName" match="Value"
use="concat(generate-id(..), '+', @name)"/>
<xsl:template match="/">
<xsl:value-of select=
"key('kValueByKeyAndName',
concat(generate-id(key('kValueByData', 'name3')/../..),
'+',
'rsv-group-name'
)
)
/@data
"/>
</xsl:template>
</xsl:stylesheet>
将此转换应用于提供的XML文档:
<setting>
<Key name="com.ahsay.afc.cpf.UserGroup" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="1328200856753" />
<Value name="rsv-group-name" inheritParentAttribute="Y" type="string" data="group 1" />
<Value name="rsv-user-type" inheritParentAttribute="Y" type="string" data="backup-user" />
<Value name="rsv-owner" inheritParentAttribute="Y" type="string" data="" />
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name1" />
</Key>
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name2" />
</Key>
</Key>
<Key name="com.ahsay.afc.cpf.UserGroup" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="1328200856753" />
<Value name="rsv-group-name" inheritParentAttribute="Y" type="string" data="group 2" />
<Value name="rsv-user-type" inheritParentAttribute="Y" type="string" data="backup-user" />
<Value name="rsv-owner" inheritParentAttribute="Y" type="string" data="" />
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name3" />
</Key>
<Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
<Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
<Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name4" />
</Key>
</Key>
</setting>
产生了想要的正确结果:
group 2