无法使用XSLT浏览XML

时间:2016-11-18 06:51:08

标签: xml xslt-2.0

我正在尝试浏览XML并使用XSLT创建HTML文件。

这是我的样本:

<?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>

我的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">
  <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>

并在那之后做更多的事情。结果一直到<div class="panel-group" id="accordion">,这实际上意味着我正在正确地访问xpath表达式。

有什么想法吗?感谢

2 个答案:

答案 0 :(得分:0)

您的XML格式不正确,因为它使用了尚未声明的名称空间前缀ns0。

也许您遗漏了名称空间声明,因为您认为它并不重要。如果是这样,那你就错了 - 命名空间至关重要。你的xsl:for-each指令选择没有命名空间的元素(监视器,消息,消息),但你的输入元素显然在命名空间中。

答案 1 :(得分:0)

供参考,这是我修复的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>