【发布时间】: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>
然后做更多的事情。结果一直到<div class="panel-group" id="accordion">,这基本上意味着我正确地点击了xpath表达式。
有什么想法吗?谢谢
【问题讨论】:
-
你说的“一直到
<div class="panel-group" id="accordion">”是什么意思?请显示实际输出和期望输出。 -
大概样式表没有做你想做的事。但我们只能猜测你想让它做什么。从不正确的代码中推断出您的需求是一种黑艺术。
-
@JimGarrison 再次阅读我写的内容确实没有意义。我想说的是,XSLT 的结果一直到行。这是最后一行。所以 for-each 不起作用。@MichaelKay 我只是想遍历 /monitor/messages/message。消息元素是可重复的。如果你“想要迭代”那是为了达到某种目的。您需要解释其目的,因为(鉴于您似乎是 XSLT 的新手),“迭代”可能是完全错误的方式。