-1

I am trying to navigate through an XML and create an HTML file using XSLT.

This is my sample:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:monitor>
    <ns0:messages>
        <ns0:message>
            <ns0:surrogate_key>R2DYSJ</ns0:surrogate_key>
            <ns0:isFailed>false</ns0:isFailed>
            <ns0:rollovers>
                <ns0:rollover>
                    <ns0:conversation_id>Rollover.53789980697.18112016110321.48027</ns0:conversation_id>
                    <ns0:part_id>5378998069748027</ns0:part_id>
                    <ns0:transaction_type>INITIATE_OUT</ns0:transaction_type>
                    <ns0:transaction_status>IRR_ACCEPTED_BY_GATEWAY</ns0:transaction_status>
                    <ns0:transferring_usi>123213</ns0:transferring_usi>
                    <ns0:receiving_usi>123213</ns0:receiving_usi>
                    <ns0:mmbr_family_name>LG</ns0:mmbr_family_name>
                    <ns0:policy_number>1005905885</ns0:policy_number>
                    <ns0:isFailed>false</ns0:isFailed>
                </ns0:rollover>
            </ns0:rollovers>
        </ns0:message>
        <ns0:message>
            <ns0:surrogate_key>R2DYTX</ns0:surrogate_key>
            <ns0:isFailed>false</ns0:isFailed>
            <ns0:rollovers>
                <ns0:rollover>
                    <ns0:conversation_id>Rollover.53789980697.18112016110321.48027.ANZ</ns0:conversation_id>
                    <ns0:part_id>5378998069748027</ns0:part_id>
                    <ns0:transaction_type>INITIATE_IN</ns0:transaction_type>
                    <ns0:transaction_status>POLICY_DETAILS_NOT_FOUND</ns0:transaction_status>
                    <ns0:transferring_usi>123213</ns0:transferring_usi>
                    <ns0:receiving_usi>123213</ns0:receiving_usi>
                    <ns0:mmbr_family_name>LG</ns0:mmbr_family_name>
                    <ns0:isFailed>false</ns0:isFailed>
                </ns0:rollover>
            </ns0:rollovers>
        </ns0:message>
    </ns0:messages>
</ns0:monitor>

My XSLT looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:output method="html"  version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template  match="/"> 
    <html>
      <head>
        <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" ></link>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      </head>
      <body>
        <div class="container">
          <h2>Rollovers Monitor</h2>
          <p><strong>Note:</strong> This dashboard show data for only the last 3 days.</p>
          <div class="panel-group" id="accordion">
            <xsl:for-each select="//monitor/messages/message">
              <xsl:choose>
                <xsl:when test="isFailed = 'true'">
                  <xsl:element name="div">
                    <xsl:attribute name="class">
                      panel panel-danger
                    </xsl:attribute>
                  </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:element name="div">
                    <xsl:attribute name="class">
                      panel panel-default
                    </xsl:attribute>
                  </xsl:element>
                </xsl:otherwise>
              </xsl:choose>
              <div class="panel-heading">
                <h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#{surrogate_key}">Surrogate Key {surrogate_key}</a></h4>
              </div>

and does more things after that. The result of this goes all the way to <div class="panel-group" id="accordion"> which essentially means I am hitting the xpath expression correctly.

Any ideas? Thanks

Felipe Caldas
  • 2,492
  • 3
  • 36
  • 55
  • What do you mean by _"goes all the way to `
    `"_? Please show the actual output and desired output.
    – Jim Garrison Nov 18 '16 at 07:07
  • 1
    Presumably the stylesheet doesn't do what you want it to do. But we can only guess what you want it to do. Inferring your requirements from incorrect code is a bit of a black art. – Michael Kay Nov 18 '16 at 10:16
  • @JimGarrison Readin again what I wrote doesnt indeed make sense. What I tried to say is that the result of the XSLT does up till the line
    . It's the last line. So the for-each is not working.
    – Felipe Caldas Nov 18 '16 at 11:50
  • @MichaelKay I simply want to iterate over /monitor/messages/message. The message element is repeatable. – Felipe Caldas Nov 18 '16 at 11:51
  • If you "want to iterate" then that is to achieve some purpose. You need to explain the purpose, because (given that you appear to be a bit of a novice with XSLT), "iterating" might be quite the wrong way to go about it. – Michael Kay Nov 18 '16 at 14:30
  • @MichaelKay yes, novice with XSLT indeed. The message structure is repeatable. I want to go through it and and create an HTML table. Just that. – Felipe Caldas Nov 19 '16 at 04:34
  • Note that your entire xsl:choose can be replaced with `
    `.
    – Michael Kay Nov 19 '16 at 14:20
  • You need to read about "Default (null) namespace in XPath. This is the most FAQ and rough stumbling block even for some experienced people. See for example *this answer*: http://stackoverflow.com/a/297310/36305 – Dimitre Novatchev Nov 20 '16 at 01:12

2 Answers2

0

Your XML is not well formed, because it uses a namespace prefix ns0 that has not been declared.

Perhaps you left out the namespace declaration because you thought it wasn't important. If so, you are badly wrong - namespaces are of vital importance. Your xsl:for-each instruction selects elements (monitor, messages, message) that are in no namespace, but your input elements are clearly in a namespace.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

For reference, this is my fixed XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:t="http://www.tibco.com/schemas/RolloversEnvironmentMonitor/Schema.xsd">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/"> 
    <html>
      <head>
        <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"></link>
      </head>
      <body>
        <div class="container">
          <h2>Rollovers Monitor</h2>
          <p><strong>Note:</strong> This dashboard show data for only the last 3 days.</p>
          <div class="panel-group" id="accordion">
            <xsl:for-each select="t:monitor/t:messages/t:message">
              <xsl:choose>
                <xsl:when test="t:isFailed = 'true'">
                  <xsl:element name="div">
                    <xsl:attribute name="class">panel panel-danger</xsl:attribute>
                  </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:element name="div">
                    <xsl:attribute name="class">panel panel-default</xsl:attribute>
                  </xsl:element>
                </xsl:otherwise>
              </xsl:choose>
              <div class="panel-heading">
                <h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#{t:surrogate_key}">Surrogate Key <xsl:value-of select="t:surrogate_key"/></a></h4>
              </div>
              <div id='{t:surrogate_key}' class="panel-collapse collapse">
                <div class="panel-body">
                  <table class="table table-hover">
                    <thead>
                      <tr>
                        <th>Transaction Type</th>
                        <th>Conversation Id</th>
                        <th>Part Id</th>
                        <th>IWS WIN</th>
                        <th>TRF USI</th>
                        <th>RCV USI</th>
                        <th>Member Last Name</th>
                        <th>Policy Number</th>
                        <th>Status</th>
                      </tr>
                    </thead>
                    <tbody>
                      <xsl:for-each select="t:rollovers/t:rollover">
                        <tr>
                          <td><xsl:value-of select="t:transaction_type"/></td>
                          <td><xsl:value-of select="t:conversation_id"/></td>
                          <td><xsl:value-of select="t:part_id"/>.30</td>
                          <td><xsl:value-of select="t:iws_win"/></td>
                          <td><xsl:value-of select="t:transferring_usi"/></td>
                          <td><xsl:value-of select="t:receiving_usi"/></td>
                          <td><xsl:value-of select="t:mmbr_family_name"/></td>
                          <td><xsl:value-of select="t:policy_number"/></td>
                          <td><xsl:value-of select="t:transaction_status"/></td>
                        </tr>
                      </xsl:for-each>
                    </tbody>
                  </table>
                </div>
              </div>
            </xsl:for-each>
          </div>
        </div> 
      </body>
      <xsl:element name="script">
        <xsl:attribute name="src">https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js</xsl:attribute>
      </xsl:element>
      <xsl:element name="script">
        <xsl:attribute name="src">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js</xsl:attribute>
      </xsl:element>
</html>
</xsl:template>
</xsl:stylesheet>
Felipe Caldas
  • 2,492
  • 3
  • 36
  • 55