XML,XSL转换

时间:2015-11-11 18:20:19

标签: jquery html xml xslt xml-parsing

在我的代码中,我将从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>

1 个答案:

答案 0 :(得分:0)

对于月份名称,请尝试:

<span class="month">
    <xsl:value-of select="$vMonthNames[number(substring-before(current()/StartDate,'/'))]/text()"/>
</span>

重要:您必须移动pMonthNames参数并将其设为样式表的子项。

对于开始时间,请尝试:

<p class="desc">
    <xsl:value-of select="ActivityName"/>
    <xsl:text> Start Time: </xsl:text>

    <xsl:variable name="h" select="floor(StartMinute div 60)" />
    <xsl:variable name="m" select="StartMinute mod 60" />

    <xsl:value-of select="($h + 11) mod 12 + 1"/>
    <xsl:value-of select="format-number($m, ':00')"/>
    <xsl:value-of select="substring(' AM PM', 1 + 3*($h > 11), 3)"/>
 </p>

加了:

  

我无法修复月份名称代码。

看看这是否适合你:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="pMonthNames">
    <name><short>JAN</short>January</name>
    <name><short>FEB</short>February</name>
    <name><short>MAR</short>March</name>
    <name><short>APR</short>April</name>
    <name><short>MAY</short>May</name>
    <name><short>JUN</short>June</name>
    <name><short>JUL</short>July</name>
    <name><short>AUG</short>August</name>
    <name><short>SEP</short>September</name>
    <name><short>OCT</short>October</name>
    <name><short>NOV</short>November</name>
    <name><short>DEC</short>December</name>
</xsl:param>

<xsl:variable name="vMonthNames" select="document('')/*/xsl:param[@name='pMonthNames']/*"/>

<xsl:template match="/Items">
    <html>
        <body>
            <ul class="event-list">
                <xsl:for-each select="Item">
                    <li>
                        <!-- ... -->
                        <span class="month">
                            <xsl:value-of select="$vMonthNames[number(substring-before(current()/StartDate,'/'))]/text()"/>
                        </span>                   
                        <!-- ... -->                        
                    </li>       
                </xsl:for-each>
            </ul>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>