I have a sequence of Color elements like this:
<Colors>
<Color Name ="AliceBlue" Hex="#F0F8FF"/>
<Color Name ="AntiqueWhite" Hex="#FAEBD7"/>
<!-- more values... -->
</Colors>
And a sequence of words:
<Words>
<Element>1px</Element>
<Element>Blue</Element>
<Element>Solid</Element>
</Words>
What is an efficient way to find where a Colors/Color/@name
attribute exactly matches a node in Words/Element/text()
, and retrieve that @name?
As @michael.hor257k suggested, you could use keys for this; assuming this sample document:
<root>
<Colors>
<Color Name ="AliceBlue" Hex="#F0F8FF"/>
<Color Name ="AntiqueWhite" Hex="#FAEBD7"/>
<Color Name="AnotherColor" Hex="123" />
<!-- more values... -->
</Colors>
<Words>
<Element>1px</Element>
<Element>Blue</Element>
<Element>AntiqueWhite</Element>
<Element>AliceBlue</Element>
</Words>
</root>
This XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:key name="colors" match="/root/Colors/Color" use="@Name" />
<xsl:template match="/root/Words/Element[key('colors', .)]">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="text()" />
</xsl:transform>
Would output the names of colors that match in both the Element
and Color
nodes. Here's the XSLTransform .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.