2015-09-08 65 views
2

我的色彩元素的这样一个顺序:XSLT 1.0:高效地比较两个节点集的匹配

<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()中的节点完全匹配,并检索该@name?

+3

这两个节点集在同一个文档中吗?如果是,请使用[键](http://www.w3.org/TR/xslt/#key)。如果不是的话,无论如何都要使用密钥 - 但设置起来会稍微复杂一些。 - P.S.实际匹配的例子会更有用,预期的输出也是如此。 - P.P.S.这里没有“十字路口”;两个集合都不共有节点。 –

+0

相应地编辑标题。 –

回答

4

由于@ michael.hor257k建议,你可以使用这个键;假设此示例文件:

<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> 

将输出的匹配在两个ElementColor节点是颜色的名称。这里是XSLTransform