Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:44:26+00:00 2026-05-27T06:44:26+00:00

Background Obtain the value from a list of XML elements using a unique identifier.

  • 0

Background

Obtain the value from a list of XML elements using a unique identifier.

Problem

Given an XML document of the form:

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

The goal is to produce the following:

  1. identify computer, network cable, printer, and USB cable
  2. unplug network cable from computer
  3. plug USB cable into printer
  4. plug USB cable into computer

I have tried the following:

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

In other words, I would like to match on <r:install> and then use the id attribute from any <r:item ... /> element. Each <r:step /> can have multiple <r:item id="X" /> references. These references correspond to the <r:item>...</r:item> elements within the <r:equipment ../> element list.

The id values for each <r:item /> are guaranteed unique.

I would like to avoid using an additional tag if possible (e.g., <r:item-ref id="X" />).

Question

Given <r:install><r:step action="unplug"><r:item id="4" /></r:step></r:install> and the following list:

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

What is the corresponding select XPath expression to get the value of USB cable when applying a template that hits upon <r:item id="4" />, which serves as a reference to said value, anywhere in the XML document?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:44:27+00:00Added an answer on May 27, 2026 at 6:44 am

    This is the ideal situation to use a key to look-up your r:item values from the r:equipment elements.

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

    So, you “use” the @id attribute to look up r:equipment/r:item elements. To use this key, assuming you are positioned on one of your r:step/r:item elements, you just do this…

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

    Here is the full 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>
    

    When applied to your source XML, the following is produced

    <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I'm trying to get obtain a unique identifier out of a computer and
Background: At my company we are developing a bunch applications that are using the
Background I am writing and using a very simple CGI-based (Perl) content management tool
Is there any function (C++, MFC) to obtain window's background color?
How do I use obtain a drawable from a Shader (such as LinearGradient )
Background I have a GridView that builds a table using an ObjectDataSource . This
I'm new to C# (from a native C++ background) and I'm trying to write
How do you obtain graphic primitives and directives from a Graphics object? Leonid Shifrin
How do i obtain aero glass box effect using css. My user can dynamically
Little background.. I'm in the process of making an OpenGL game using Java and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.