2011-01-25 59 views
0

如何匹配XML文檔中的兩個單獨的數字?我的XML文檔中有多個<PgIndexElementInfo>元素,每個元素代表一個不同的導航元素,每個導航元素都有一個唯一的<ID>。稍後在文件<PageID>指定一個數字有時匹配上面使用<ID>。我怎樣才能將<PageID>與上面指定的<ID>匹配?將PAGEID與PAGEID匹配到一個元素ID

<Element> 
    <Content> 
     <PgIndexElementInfo> 
      <ElementData> 
       <Items> 
        <PgIndexElementItem> 
         <ID>1455917</ID> 
        </PgIndexElementItem> 
       </Items> 
      </ElementData> 
     </PgIndexElementInfo> 
    </Content> 
</Element> 
<Element> 
    <Content> 
     <CustomElementInfo> 
      <PageID>1455917</PageID> 
     </CustomElementInfo> 
    </Content> 
</Element> 

編輯:

我加了下面的解決我的代碼。存在的xsl:apply-templates用於重新創建在HTML和XML之間丟失的嵌套列表。我現在需要做的是將PageID與<PgIndexElementItem>的ID相匹配,然後將CSS類添加到它所屬的<ul>中。我希望這是有道理的。

<xsl:key name="kIDByValue" match="ID" use="."/> 
<xsl:template match="PageID[key('kIDByValue',.)]"> 
    <xsl:apply-templates select="//PgIndexElementItem[not(contains(Description, '.'))]" /> 
</xsl:template> 

<xsl:template match="PgIndexElementItem"> 
    <li> 
    <a href="{ResolvedURL/Absolute}"><xsl:value-of select="Title"/></a> 
    <xsl:variable name="prefix" select="concat(Description, '.')"/> 
    <xsl:variable name="childOptions" 
     select="../PgIndexElementItem[starts-with(Description, $prefix) 
     and not(contains(substring-after(Description, $prefix), '.'))]"/> 
    <xsl:if test="$childOptions"> 
     <ul> 
     <xsl:apply-templates select="$childOptions" /> 
     </ul> 
    </xsl:if> 
    </li> 
</xsl:template> 

回答

3

用於處理交叉引用的XSLT方法是使用鍵。

匹配:匹配PageID元素的規則,它已被ID元素引用。

<xsl:key name="kIDByValue" match="ID" use="."/> 
<xsl:template match="PageID[key('kIDByValue',.)]"> 
    <!-- Template content --> 
</xsl:template> 

選擇:A表達選擇與特定值的每個PageID元件。

​​
+0

+1對於正確的答案 – 2011-01-25 20:53:23

相關問題