Friday, July 5, 2013

XML/XSLT Parsing your business data which is in xml using XSLT -by Rayudu Addagarla

In today’s world data is everywhere and xml could be one source and most common source as feed from data vendors etc.

 

Say suppose you have some xml data to show for business users and a requirement to show in the webpage or parse with XSLT to use in some application.

This below shows how to parse daily averages of Federal Funds available for public.

 

I just showed to use xsl:for-each and show in tables. You can apply any XSLT rules to filter xsl:if, xsl:choose, xsl:when etc.

Shows how to read elements and attributes and xml which has (:colon) namespaces ex: ff:Series

<ff:Series AVAILABILITY="A" DECIMALS="2" FF_METHOD="D" DISCLAIMER="G" TIME_FORMAT="P1D">

 

http://www.newyorkfed.org/xml/data/ff/ffd.xml

 

How would you parse this with XSLT if you want to just display on your page based on some rules etc.

 

 

XML (ffd.xml)

XSL

<?xml version="1.0" encoding="ISO-8859-1"?>

<UtilityData>

<Header>

<ID>FFD</ID>

<Test>false</Test>

<Name xml:lang="en">Federal Funds daily averages</Name>

<Prepared>2013-07-05</Prepared>

<Sender id="FRBNY">

<Name xml:lang="en">Federal Reserve Bank of New York</Name>

<Contact>

<Name xml:lang="en">George Matthes</Name>

<Email>george.matthes@ny.frb.org</Email>

</Contact>

</Sender>

</Header>

<ff:DataSet>

<ff:Series AVAILABILITY="A" DECIMALS="2" FF_METHOD="D" DISCLAIMER="G" TIME_FORMAT="P1D">

<ffbase:Key>

<base:FREQ>D</base:FREQ>

<base:RATE>FF</base:RATE>

<base:MATURITY>O</base:MATURITY>

<ffbase:FF_SCOPE>D</ffbase:FF_SCOPE>

</ffbase:Key>

<ff:Obs OBS_CONF="F" OBS_STATUS="A">

<base:TIME_PERIOD>2013-07-02</base:TIME_PERIOD>

<base:OBS_VALUE>0.1</base:OBS_VALUE>

</ff:Obs>

<ff:Obs OBS_CONF="F" OBS_STATUS="A">

<base:TIME_PERIOD>2013-07-01</base:TIME_PERIOD>

<base:OBS_VALUE>0.1</base:OBS_VALUE>

</ff:Obs>

<ff:Obs OBS_CONF="F" OBS_STATUS="A">

<base:TIME_PERIOD>2013-06-28</base:TIME_PERIOD>

<base:OBS_VALUE>0.07</base:OBS_VALUE>

</ff:Obs>

<ff:Obs OBS_CONF="F" OBS_STATUS="A">

<base:TIME_PERIOD>2013-06-27</base:TIME_PERIOD>

<base:OBS_VALUE>0.09</base:OBS_VALUE>

</ff:Obs>

</ff:Series>

</ff:DataSet>

</UtilityData>

<?xml version="1.0" encoding="ISO-8859-1"?>

<!—Author Rayudu Addagarla, Tested in W3schools Try Yourself Editor -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

<xsl:template match="/">

  <html>

  <body>

  <h3>Federal Reserve Bank of New York</h3>

    

 <xsl:for-each select="UtilityData/Header">

  <b>Name :</b><xsl:value-of select="Name" />

   <br/><b>Sender :</b><xsl:value-of select="Sender/@id" />

  </xsl:for-each>

<br/>

    <table border="1">

      <tr bgcolor="#9acd32">

        <th>Name</th>

        <th>Email</th>

      </tr>

      <xsl:for-each select="UtilityData/Header/Sender/Contact">

      <tr>

        <td><xsl:value-of select="Name"/></td>

        <td><xsl:value-of select="Email"/></td>

      </tr>

      </xsl:for-each>

    </table>

<br/>

<table border="1">

<caption style="text-align:left"><b>Series</b></caption>

      <tr bgcolor="#9acd32">

        <th>Availability </th>

        <th>Decimals</th>

        <th>FF Method</th>

        <th>Disclaimer</th>

        <th>Maturity</th>

      </tr>

<xsl:for-each select="//*[local-name() = 'DataSet']">

<tr>

        <td> <xsl:value-of select="Series/@AVAILABILITY" />   </td>

        <td>   <xsl:value-of select="Series/@DECIMALS" />   </td>

        <td>   <xsl:value-of select="Series/@FF_METHOD" />   </td>

        <td>   <xsl:value-of select="Series/@DISCLAIMER" />   </td>

        <td>   <xsl:value-of select="Series/Key/MATURITY" />   </td>

</tr>

  </xsl:for-each>

</table> <br/>

<table border="1">

      <tr bgcolor="#9acd32">

        <th>OBS Conf</th>

       <th>OBS Status</th>

        <th>Time Period</th>

        <th>OBS Value</th>

      </tr>

      <xsl:for-each select="//*[local-name() = 'Obs']">

      <tr>

        <td><xsl:value-of select="@OBS_CONF"/></td>

        <td><xsl:value-of select="@OBS_STATUS"/></td>

        <td><xsl:value-of select="TIME_PERIOD"/></td>

        <td><xsl:value-of select="OBS_VALUE"/></td>

 

      </tr>

      </xsl:for-each>

    </table>

 

  </body>

  </html>

</xsl:template>

</xsl:stylesheet>

 

 

Rayudu Addagarla

BPM Specialist

Keywords: [Appian BPM, Java, JS, JSON, XML, XSLT, Web Development, JSP, Spring, JBOSS]

No comments:

Post a Comment