簡體   English   中英

XSLT 1.0:有效地比較兩個節點集是否匹配

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

我有一系列這樣的Color元素:

<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的有效方法是什么?

正如@ 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>

將輸出在“ Element和“ Color節點中都匹配的顏色名稱。 這是XSLTransform

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM