【发布时间】:2016-02-12 22:30:15
【问题描述】:
在我的代码中,我从 XML 中获取开始日期(2015 年 11 月 31 日)并将其分解为单独的月份和日期。我得到它很好。但是,当我尝试将月份编号转换为月份名称时。我使用的代码不起作用。我也在尝试在开始时间添加 AM/Pm。
XSL Content enter code here
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="metadata">
<xsl:template match="/">
<html>
<head>
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
window.alert = function(){};
var defaultCSS = document.getElementById('bootstrap-css');
function changeCSS(css){
if(css) $('head > link').filter(':first').replaceWith('<link rel="stylesheet" href="'+ css +'" type="text/css" />');
else $('head > link').filter(':first').replaceWith(defaultCSS);
}
$( document ).ready(function() {
var iframe_height = parseInt($('html').height());
window.parent.postMessage( iframe_height, 'http://bootsnipp.com');
});
</script>
<style>
@import url("http://fonts.googleapis.com/css? family=Lato:100,300,400,700,900,400italic");
@import url("//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css");
body {
padding: 60px 0px;
background-color: rgb(220, 220, 220);
}
.event-list {
list-style: none;
font-family: 'Lato', sans-serif;
margin: 0px;
padding: 0px;
}
.event-list > li {
background-color: rgb(255, 255, 255);
box-shadow: 0px 0px 5px rgb(51, 51, 51);
box-shadow: 0px 0px 5px rgba(51, 51, 51, 0.7);
padding: 0px;
margin: 0px 0px 20px;
}
.event-list > li > time {
display: inline-block;
width: 100%;
color: rgb(255, 255, 255);
background-color: blue;
padding: 5px;
text-align: center;
text-transform: uppercase;
}
.event-list > li:nth-child(even) > time {
background-color: rgb(165, 82, 167);
}
.event-list > li > time > span {
display: none;
}
.event-list > li > time > .day {
display: block;
font-size: 28pt;
font-weight: 100;
line-height: 1;
}
.event-list > li time > .month {
display: block;
font-size: 12pt;
font-weight: 450;
line-height: 1;
}
.event-list > li > .info {
padding-top: 5px;
text-align: center;
}
.event-list > li > .info > .title {
font-size: 10pt;
font-weight: 700;
margin: 0px;
}
.event-list > li > .info > .desc {
font-size: 10pt;
font-weight: 300;
margin: 0px;
}
@media (min-width: 350px) {
.event-list > li {
position: relative;
display: block;
width: 100%;
height: 63px;
padding: 0px;
}
.event-list > li > time,
.event-list > li > img {
width: 60px;
float: left;
}
.event-list > li > .info {
position: relative;
height: 60px;
text-align: left;
padding-right: 40px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="[ col-xs-12 col-sm-offset-2 col-sm-8 ]">
<xsl:for-each select="Items/Item">
<ul class="event-list">
<li>
<time >
<span class="day"><xsl:value-of select="substring- before(substring-after(StartDate,'/'),'/')"/></span>
<!--<span class="month"><xsl:value-of select="substring-before(StartDate,'/')"/></span> -->
<xsl:param name="pMonthNames"/>
<xsl:choose>
<xsl:when test="$month = 1">Jan</xsl:when>
<xsl:when test="$month = 2">Feb</xsl:when>
<xsl:when test="$month = 3">Mar</xsl:when>
<xsl:when test="$month = 4">Apr</xsl:when>
<xsl:when test="$month = 5">May</xsl:when>
<xsl:when test="$month = 6">Jun</xsl:when>
<xsl:when test="$month = 7">Jul</xsl:when>
<xsl:when test="$month = 8">Aug</xsl:when>
<xsl:when test="$month = 9">Sep</xsl:when>
<xsl:when test="$month = 10">Oct</xsl:when>
<xsl:when test="$month = 11">Nov</xsl:when>
<xsl:when test="$month = 12">Dec</xsl:when>
<xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise>
</xsl:choose>
<xsl:variable name="vMonthNames" select="document('')/*/xsl:param[@name='pMonthNames']/*"/>
<span class="month">
<xsl:value-of select="$vMonthNames[number(substring-before(current()/StartDate,'/'))]/text()"/>
</span>
</time>
<div class="info">
<h2 class="title"><xsl:value-of select="ParentActivityName"/></h2>
<p class="desc"><xsl:value-of select="ActivityName"/>
<xsl:text> Start Time:</xsl:text><xsl:value-of select="floor((StartMinute div 60)- (12 * ((StartMinute div 60) > 12)) + (12 * ((StartMinute div 60) = 0)))"/></p>
<!-- <p class="desc"><xsl:text> End Time:</xsl:text><xsl:value-of select="floor((EndMinute div 60)- (12 * ((EndMinute div 60) > 12)) + (12 * ((EndMinute div 60) = 0)))"/></p> -->
</div>
</li>
</ul>
</xsl:for-each>
</div>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML 内容 在此输入代码
<?xml version="1.0" encoding="UTF-8" ?>
<Items>
<Item>
<ActivityName>Eastern New Mexico - SPEC</ActivityName>
<ParentActivityName>Fall 2015 Volleyball</ParentActivityName>
<Description></Description>
<StartDate>11/13/2015 12:00:00 AM</StartDate>
<EndDate>11/13/2015 12:00:00 AM</EndDate>
<StartMinute>1140</StartMinute>
<EndMinute>1200</EndMinute>
<ActivityTypeCode>2</ActivityTypeCode>
<CampusName></CampusName>
<BuildingCode></BuildingCode>
<RoomNumber></RoomNumber>
<RoomName></RoomName>
</Item>
</Items>
【问题讨论】:
-
从
https://astraweb.tamuk.edu/加载脚本的主 HTML 文档在哪里?否则同源策略将不允许脚本访问任何 iframe 内容。 -
使用 XMLHttpRequest 加载文档并使用 XSLTProcessor 执行 XSLT 转换。将一些 XML 构造为字符串并将其作为 HTML 放入 iframe 将不会应用任何 XSLT 样式表。
-
我有 XSL 文档,但是我将如何通过链接加载 XML 文档。这意味着我没有 XML 文档。它是在 ifram 上显示 XML 格式的链接
-
您拥有该 iframe 的 URL,因此您当然可以使用 XMLHttpRequest 加载它。当然,在生成 XML 时,整个问题可以在服务器上更好地解决,方法是直接在那里应用 XSLT,或者至少在生成 XML 并将其发送到浏览器时直接将
<?xml-stylesheet?>处理指令放入 XML 中。
标签: jquery html xml xslt xml-parsing