Link to home
Start Free TrialLog in
Avatar of kanishkpanwar
kanishkpanwar

asked on

XSLT - Extract the value of an element dynamically

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
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of kanishkpanwar
kanishkpanwar

ASKER

Thanks a ton!