Solved

XSLT - Extract the value of an element dynamically

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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

685 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