Solved

XSLT - Extract the value of an element dynamically

Posted on 2007-11-21
3
183 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

705 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

18 Experts available now in Live!

Get 1:1 Help Now