【问题标题】:Cannot navigate through an XML using XSLT无法使用 XSLT 浏览 XML
【发布时间】:2016-11-18 06:51:08
【问题描述】:

我正在尝试浏览 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>

然后做更多的事情。结果一直到&lt;div class="panel-group" id="accordion"&gt;,这基本上意味着我正确地点击了xpath表达式。

有什么想法吗?谢谢

【问题讨论】:

  • 你说的“一直到&lt;div class="panel-group" id="accordion"&gt;”是什么意思?请显示实际输出和期望输出。
  • 大概样式表没有做你想做的事。但我们只能猜测你想让它做什么。从不正确的代码中推断出您的需求是一种黑艺术。
  • @JimGarrison 再次阅读我写的内容确实没有意义。我想说的是,XSLT 的结果一直到
    行。这是最后一行。所以 for-each 不起作用。
  • @MichaelKay 我只是想遍历 /monitor/messages/message。消息元素是可重复的。
  • 如果你“想要迭代”那是为了达到某种目的。您需要解释其目的,因为(鉴于您似乎是 XSLT 的新手),“迭代”可能是完全错误的方式。

标签: xml xslt-2.0


【解决方案1】:

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

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

【讨论】:

  • 这很有意义,实际上。让我试着把命名空间放进去看看会发生什么!
  • 谢谢。修复命名空间和其他一些东西后,现在一切正常
【解决方案2】:

作为参考,这是我固定的 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>

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签