【问题标题】:Obtain an XML element's value given an arbitrary attribute index在给定任意属性索引的情况下获取 XML 元素的值
【发布时间】:2011-12-02 08:41:28
【问题描述】:

背景

使用唯一标识符从 XML 元素列表中获取值。

问题

给定一个 XML 文档的格式:

<root xmlns:r="http://internet.com/network">
<r:equipment label="network">
  <r:item id="1">computer</r:item>
  <r:item id="2">network cable</r:item>
</r:equipment>
<r:equipment label="peripheral">
  <r:item id="3">printer</r:item>
  <r:item id="4">USB cable</r:item>
</r:equipment>

<r:install>
  <r:step action="identify"><r:item id="1" />, <r:item id="2" />, <r:item id="3" />, and <r:item id="4" /></r:step>
  <r:step action="unplug"><r:item id="2" /> from <r:item id="1" /></r:step>
  <r:step action="plug"><r:item id="4" /> into <r:item id="3" /></r:step>
  <r:step action="plug"><r:item id="4" /> into <r:item id="1" /></r:step>
</r:install>
</root>

目标是产生以下内容:

  1. 识别计算机、网络电缆、打印机和 USB 电缆
  2. 从计算机上拔下网线
  3. 将 USB 数据线插入打印机
  4. 将 USB 数据线插入计算机

我尝试了以下方法:

<xsl:template match="r:install">
<p>
Installation
</p>
  <ol>
  <xsl:for-each select="r:step">
    <li><xsl:apply-templates select="." /></li>
  </xsl:for-each>
  </ol>
</xsl:template>

<xsl:template match="//root/r:install/r:step/r:item">
  ID: <xsl:value-of select="@id" />
</xsl:template>

换句话说,我想匹配&lt;r:install&gt;,然后使用来自任何&lt;r:item ... /&gt; 元素的id 属性。每个&lt;r:step /&gt; 可以有多个&lt;r:item id="X" /&gt; 引用。这些引用对应于&lt;r:equipment ../&gt; 元素列表中的&lt;r:item&gt;...&lt;/r:item&gt; 元素。

每个&lt;r:item /&gt;id 值保证是唯一的。

如果可能,我想避免使用额外的标签(例如,&lt;r:item-ref id="X" /&gt;)。

问题

给定&lt;r:install&gt;&lt;r:step action="unplug"&gt;&lt;r:item id="4" /&gt;&lt;/r:step&gt;&lt;/r:install&gt; 和以下列表:

<r:equipment label="peripheral">
  <r:item id="3">printer</r:item>
  <r:item id="4">USB cable</r:item>
</r:equipment>

当在 XML 文档中的任何位置应用一个模板命中 &lt;r:item id="4" /&gt;(用作对所述值的引用)时,对应的 select XPath 表达式是什么来获取 USB cable 的值?

谢谢!

【问题讨论】:

    标签: xml xslt indexing


    【解决方案1】:

    这是使用键从 r:equipment 元素中查找 r:item 值的理想情况。

    <xsl:key name="equipment" match="r:equipment/r:item" use="@id" />
    

    因此,您“使用” @id 属性来查找 r:equipment/r:item 元素。要使用此键,假设您位于 r:step/r:item 元素之一,您只需执行此操作...

    <xsl:value-of select="key('equipment', @id)" />
    

    这是完整的 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r="http://internet.com/network" exclude-result-prefixes="r">
       <xsl:output method="html" indent="yes"/>
    
       <xsl:key name="equipment" match="r:equipment/r:item" use="@id" />
    
       <xsl:template match="/root">
          <xsl:apply-templates select="r:install"/>
       </xsl:template>
    
       <xsl:template match="r:install">
          <p> Installation </p>
          <ol>
             <xsl:apply-templates select="r:step" />
          </ol>
       </xsl:template>
    
       <xsl:template match="r:step">
          <li>
             <xsl:value-of select="concat(@action, ' ')" />
             <xsl:apply-templates select="node()" />
          </li>
       </xsl:template>
    
       <xsl:template match="r:item"> 
          <xsl:value-of select="key('equipment', @id)" />
       </xsl:template>
    </xsl:stylesheet>
    

    当应用于您的源 XML 时,会生成以下内容

    <p> Installation </p>
    <ol>
    <li>identify computer, 
                network cable, 
                printer, and 
                USB cable</li>
    <li>unplug network cable from 
                computer</li>
    <li>plug USB cable into 
                printer</li>
    <li>plug USB cable into 
                computer</li>
    </ol>
    

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2013-04-30
      • 2011-05-07
      • 1970-01-01
      相关资源
      最近更新 更多