简体   繁体   English

XSLT 1.0:有效地比较两个节点集是否匹配

[英]XSLT 1.0: Efficiently Compare Two Nodesets for a Match

I have a sequence of Color elements like this: 我有一系列这样的Color元素:

<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? 找到Colors/Color/@name属性与Words/Element/text()中的节点完全匹配并检索该@name的有效方法是什么?

As @michael.hor257k suggested, you could use keys for this; 正如@ michael.hor257k所建议的那样,您可以为此使用密钥。 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: 此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. 将输出在“ Element和“ Color节点中都匹配的颜色名称。 Here's the XSLTransform . 这是XSLTransform

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM