2012-01-24 57 views
1

我有以下XML:爲什麼不工作我的XSL模板

<?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有問題嗎?

回答

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)"/> 
+0

我認爲這是它:) –

+0

你明白爲什麼需要這種變化?當你編寫相對路徑表達式時,你總是需要知道上下文節點是什麼 - 也就是你從哪裏選擇。 –

+0

謝謝@MichaelKay - 我現在剛剛學習XSL,這使得它更清晰一些 – Anthony

相關問題