作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个像这样的颜色元素序列:
<Colors>
<Color Name ="AliceBlue" Hex="#F0F8FF"/>
<Color Name ="AntiqueWhite" Hex="#FAEBD7"/>
<!-- more values... -->
</Colors>
还有一个单词序列:
<Words>
<Element>1px</Element>
<Element>Blue</Element>
<Element>Solid</Element>
</Words>
找到 Colors/Color/@name
属性与 Words/Element/text()
中的节点完全匹配并检索该节点的有效方法是什么?姓名?
最佳答案
正如@michael.hor257k 所建议的,您可以为此使用 key ;假设这个示例文档:
<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>
这个 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>
将输出在 Element
和 Color
节点中匹配的颜色名称。这是 XSLTransform .
关于xml - XSLT 1.0 : Efficiently Compare Two Nodesets for a Match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32463684/
我是一名优秀的程序员,十分优秀!