为什么我的XSL模板不起作用

时间:2012-01-24 05:28:09

标签: xml xslt

我有以下XML:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfTrafficResponseIncident xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <TrafficResponseIncident>
    <IncidentReportId>220</IncidentReportId>
    <IncidentNumber>137</IncidentNumber>
    <IncidentDate>2012-01-23T12:18:40.027</IncidentDate>
    <TMCRequestedFlag>true</TMCRequestedFlag>
    <TMCRequestedTime>2012-01-23T23:18:00</TMCRequestedTime>
    <TRUDetectedFlag>false</TRUDetectedFlag>
    <TRUDetectedTime xsi:nil="true"/>
    <SiteArrivalTime>2012-01-23T23:59:00</SiteArrivalTime>
    <SiteDepartedTime>2012-01-23T00:18:00</SiteDepartedTime>
    <Address>34-64 Queen St</Address>
    <Suburb>Brisbane</Suburb>
    <SecondaryRef>Lat: -27.470933  Lon: 153.023502</SecondaryRef>
    <DirectionId>1</DirectionId>
    <AssetOwnerId xsi:nil="true"/>
    <BagsOfKittyLitterUsed/>
    <AmountOfFuelAdded/>
    <QuickClearanceTowFlag>false</QuickClearanceTowFlag>
    <NumberOfPhotosAttached>0</NumberOfPhotosAttached>
    <CreatedBy>akeller</CreatedBy>
    <CreatedDateTime>2012-01-23T12:19:14.08</CreatedDateTime>
    <UpdatedDateTime xsi:nil="true"/>
    <CurrentFlag>true</CurrentFlag>
    <Online>false</Online>
  </TrafficResponseIncident>
</ArrayOfTrafficResponseIncident>

我尝试使用以下XSL对其进行转换,但<xsl:if test="count(*/TrafficResponseIncident) > 0">始终返回0.

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ms="urn:schemas-microsoft-com:xslt"
  xmlns:dt="urn:schemas-microsoft-com:datatypes">

  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:template match="/ArrayOfTrafficResponseIncident">
    <xsl:if test="count(*/TrafficResponseIncident) > 0">
      <xsl:value-of select="count(*/TrafficResponseIncident)"/>
      <table class='searchresults'>
        <tr class='header'>
          <th class='center'>Incident Number</th>
          <th>Date/Time</th>
          <th>Type(s)</th>
          <th>Location</th>
          <th>Created By</th>
        </tr>
        <xsl:for-each select="*/TrafficResponseIncident">
          <tr>
            <td>
              <xsl:value-of select="IncidentNumber"/>
            </td>
            <td>
              <xsl:value-of select="ms:format-date(IncidentDate, 'dd/MM/yyyy')"/>
            </td>
            <td>
            </td>
            <td>
              <xsl:value-of select="Address"/>
              <br/>
              <xsl:value-of select="Suburb"/>
            </td>
            <td>
              <xsl:value-of select="CreatedBy"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if>
    <xsl:if test="count(*/TrafficResponseIncident) = 0">
      <table class='searchresults'>
        <tr class='header'>
          <th class='center'>Incident Number</th>
          <th>Date/Time</th>
          <th>Type(s)</th>
          <th>Location</th>
          <th>Submitted By</th>
        </tr>
        <tr>
          <td colspan='5'>
            <div class='noDataFound'>No incident reports found matching the search criteria</div>
          </td>
        </tr>
      </table>      
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

我的XSL有什么问题?

1 个答案:

答案 0 :(得分:2)

删除所有*/前缀。这有效:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ms="urn:schemas-microsoft-com:xslt"
  xmlns:dt="urn:schemas-microsoft-com:datatypes">

  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:template match="/ArrayOfTrafficResponseIncident">
    <xsl:if test="count(TrafficResponseIncident) > 0">
      <xsl:value-of select="count(TrafficResponseIncident)"/>
      <table class='searchresults'>
        <tr class='header'>
          <th class='center'>Incident Number</th>
          <th>Date/Time</th>
          <th>Type(s)</th>
          <th>Location</th>
          <th>Created By</th>
        </tr>
        <xsl:for-each select="TrafficResponseIncident">
          <tr>
            <td>
              <xsl:value-of select="IncidentNumber"/>
            </td>
            <td>
              <xsl:value-of select="ms:format-date(IncidentDate, 'dd/MM/yyyy')"/>
            </td>
            <td>
            </td>
            <td>
              <xsl:value-of select="Address"/>
              <br/>
              <xsl:value-of select="Suburb"/>
            </td>
            <td>
              <xsl:value-of select="CreatedBy"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if>
    <xsl:if test="count(TrafficResponseIncident) = 0">
      <table class='searchresults'>
        <tr class='header'>
          <th class='center'>Incident Number</th>
          <th>Date/Time</th>
          <th>Type(s)</th>
          <th>Location</th>
          <th>Submitted By</th>
        </tr>
        <tr>
          <td colspan='5'>
            <div class='noDataFound'>No incident reports found matching the search criteria</div>
          </td>
        </tr>
      </table>      
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

并产生以下内容:

1<table class="searchresults">
    <tr class="header">
        <th class="center">Incident Number</th>
        <th>Date/Time</th>
        <th>Type(s)</th>
        <th>Location</th>
        <th>Created By</th>
    </tr>
    <tr>
        <td>137</td>
        <td>01/23/2012</td>
        <td />
        <td>34-64 Queen St<br>Brisbane</td>
        <td>akeller</td>
    </tr>
</table>

您可能希望在开头修复“1”。猜测这只是为了调试而留在那里:

<xsl:value-of select="count(TrafficResponseIncident)"/>