2012-01-24 16 views
1

なぜ私のXSLテンプレートが機能していません。私は、次の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に間違っていますか?

答えて

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

関連する問題