XSLT - Extract the value of an element dynamically

Posted on 2007-11-21
Last Modified: 2013-11-18

I am trying to extract the value of an element in an XML. In the future, this can be any XML and any element. Element name is passed as a parameter to the XSL. I am using xerces. Given below is the XML and XSL that I wrote and ofcourse, it does not work. Please suggest. FYI, I am trying to retrieve the value for MAIL_OUT_PROCESS_ID (for now).


<?xml version="1.0"?>


<xsl:stylesheet version="2.0"
      <xsl:output method="text" indent="yes" encoding="UTF-8" />

      <xsl:param name="elementName" />

      <xsl:template match="*">
            <xsl:value-of select="local-name()" />
            <xsl:if test="local-name()=$elementName">
                  <xsl:value-of select="." />
      <xsl:template match="comment()" />
      <xsl:template match="processing-instruction()" />

      <xsl:template match="text()" />


Question by:kanishkpanwar
  • 2
LVL 60

Accepted Solution

Geert Bormans earned 500 total points
ID: 20328098
just change this one template

    <xsl:template match="*">
            <xsl:when test="local-name()  = $elementName">
                <xsl:value-of select="." />
LVL 60

Expert Comment

by:Geert Bormans
ID: 20328162
the problem you were having is that your template for match="*" stopped after processing the root node
There was no continuation in your stylesheet

I would do this a lot simpler though

<xsl:stylesheet version="2.0"
    <xsl:output method="text" indent="yes" encoding="UTF-8" />
    <xsl:param name="elementName" ></xsl:param>
    <xsl:template match="/">
        <xsl:value-of select="//*[local-name() =  $elementName]"/>



Author Closing Comment

ID: 31410361
Thanks a ton!

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 20
The Best Website Creation Tools 14 53
XML response optional elements 12 37
Make Line Items Look Good Over Mobile 2 52
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now