Solved

XSLT - Extract the value of an element dynamically

Posted on 2007-11-21
3
189 Views
Last Modified: 2013-11-18
Hello,

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

<?xml version="1.0"?>
<MAILOUTS>
 <MAILOUT>
  <MAIL_OUT_ID>1234</MAIL_OUT_ID>
  <MAIL_OUT_PROCESS_ID>21</MAIL_OUT_PROCESS_ID>
  <PERSON_ID>835756</PERSON_ID>
  <PERSON_NBR>28366</PERSON_NBR>
  <FIRST_NAME>RONNIE</FIRST_NAME>
  <MIDDLE_NAME>JAMES</MIDDLE_NAME>
  <LAST_NAME>DIO</LAST_NAME>
  <ADDRESS_LINE_1>666 HELL STREET</ADDRESS_LINE_1>
  <ADDRESS_LINE_2/>
  <CITY>FORT VALLEY</CITY>
  <STATE>GA</STATE>
  <ZIP5>30320</ZIP5>
  <ZIP4>1776</ZIP4>
  <COUNTY>UNKNOWN</COUNTY>
 </MAILOUT>
</MAILOUTS>

---------------------------------------------------------------------------------------------------------------------------------------

XSL
----------
<xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <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:if>  
      </xsl:template>
      
      <xsl:template match="comment()" />
      <xsl:template match="processing-instruction()" />

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

</xsl:stylesheet>





Thanks,
Kanishk
0
Comment
Question by:kanishkpanwar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

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

    <xsl:template match="*">
        <xsl:choose>
            <xsl:when test="local-name()  = $elementName">
                <xsl:value-of select="." />
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
0
 
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"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <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]"/>
    </xsl:template>
 </xsl:stylesheet>

cheers

Geert
0
 

Author Closing Comment

by:kanishkpanwar
ID: 31410361
Thanks a ton!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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