Solved

XSLT - Extract the value of an element dynamically

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unattended XML settings 4 99
How to get statistic's from a competitors website?! 1 83
XML Data Missing in PHP SimpleXML 8 44
Excel file not created as expected 7 46
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…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

948 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

22 Experts available now in Live!

Get 1:1 Help Now