Sum number of div in the html table xslt

2019-08-12 15:39发布

问题:

I need help with counting of div in table I need sum div in whole table and the result of sum put below table. Here is my output of table:

<html>
          <body>
             <h2>Designated Points</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Coordinates</th>
                        <th>NAME</th>
                    </tr>
                    <tr>
                        <td><div>51.111217 4.5630126</div></td>
                        <td>EKELEE</td>
                    </tr>
                    <tr>
                        <td>
                            <div>50.8527128 3.1472122</div></td>
                        <td>EB1212S14</td>
                    </tr>
                    <tr>
                        <td>
                            <div>50.4812972 3.811225</div><br>
                             <div>50.4351228 3.9531289</div><br>
                             <div>50.5012278 3.912425</div><br>
                             <div>50.5312528 3.9312611</div><br>
                             <div>50.63806 3.66167</div><br>
                             <div>50.6231289 3.6461211</div><br>
                             <div>50.5121722 3.731275</div><br>
                             <div>50.4128972 3.811225</div><br>
        </td>
         </tr>
                </table>
            </body>
        </html>

Thank you for your help.

Here is my input and I dont know how to count all div in the table in the different templates:

<xsl:template match="/">
        <html>
            <body>
                <h2>Designated Points</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Coordinates</th>
                        <th>Designator</th>
                    </tr>

    <xsl:for-each select="message:AIXMBasicMessage/message:hasMember/aixm:Airspace/aixm:timeSlice/aixm:AirspaceTimeSlice">

    <tr>
        <td>
             <xsl:for-each select=".//gml:pos">
                <div>   
                    <xsl:value-of select="."/>
              </div>    
              </xsl:for-each>

                <xsl:for-each select=".//gml:posList">   
                    <xsl:call-template name="split"/>   
               </xsl:for-each>
       </td>    

       <td> 
         <xsl:value-of select="aixm:designator"/>    
       </td>

     </tr>
    </xsl:for-each>
   </table>
  </body>
 </html>

</xsl:template>

<xsl:template match="table" name="count">
<xsl:copy-of select="."/>
<xsl:value-of select="count(.//div)"/>
</xsl:template>


    <xsl:template match="text()" name="split">
        <xsl:param name="pText" select="."/>
        <xsl:if test="normalize-space($pText)">
            <xsl:variable name="vText" select="concat(normalize-space($pText), ' ')"/>

            <div>    
            <xsl:value-of select="substring-before($vText, ' ')"/>
            <xsl:value-of select="concat(' ', substring-before(substring-after($vText, ' '), ' '), '&#xA;')"/>
              </div>
            <br/>
            <xsl:call-template name="split">
                <xsl:with-param name="pText" select="substring-after(substring-after($vText, ' '), ' ')"/>
            </xsl:call-template>

        </xsl:if>
    </xsl:template> 

I need count every point from gml:pos and every splitted points from gml:posList

Here is a fake data:

<?xml version="1.0" encoding="UTF-8"?> <AIXMBasicMessage
 xmlns="http://www.aixm.aero/schema/5.1/message"
 xmlns:gml="http://www.opengis.net/gml/3.2"
 xmlns:aixm="http://www.aixm.aero/schema/5.1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:gts="http://www.isotc211.org/2005/gts"
 xmlns:gco="http://www.isotc211.org/2005/gco"
xmlns:adr="http://www.aixm.aero/schema/5.1/extensions/EUR/ADR"
 xmlns:gmd="http://www.isotc211.org/2005/gmd"
 xmlns:xlink="http://www.w3.org/1999/xlink" gml:id="uniqueId"
 xsi:schemaLocation="http://www.aixm.aero/schema/5.1/message
 http://www.aixm.aero/schema/5.1/message/AIXM_BasicMessage.xsd
 http://www.aixm.aero/schema/5.1/extensions/EUR/ADR
 http://www.aixm.aero/schema/5.1/extensions/EUR/ADR18.5/ADR_Features.xsd">
 <hasMember>        <aixm:Airspace
 xmlns="http://www.aixm.aero/schema/5.1/message"
 gml:id="uuid.b6950093-ae6d-4ef4-b7bf-c30074810e72">            <gml:identifier
 codeSpace="urn:uuid:">b6950093-ae6d-4ef4-b7bf-c30074810e72</gml:identifier>
            <gml:boundedBy xsi:nil="true"/>             <aixm:timeSlice owns="false">
                <aixm:AirspaceTimeSlice gml:id="uuid.d3c353b4-0bbc-4035-91df-24b342c56497">
                    <gml:validTime xlink:type="simple" owns="false">
                        <gml:TimePeriod gml:id="uuid.d3065abe-6c92-4da0-8455-e498b70f48d1" frame="#ISO-8601">
                            <gml:beginPosition frame="#ISO-8601">2015-06-25T00:00:00Z</gml:beginPosition>
                            <gml:endPosition indeterminatePosition="unknown" frame="#ISO-8601"/>
                        </gml:TimePeriod>
                    </gml:validTime>
                    <aixm:interpretation>BASELINE</aixm:interpretation>
                    <aixm:sequenceNumber>2</aixm:sequenceNumber>
                    <aixm:correctionNumber>0</aixm:correctionNumber>
                    <aixm:featureLifetime xlink:type="simple" owns="false">
                        <gml:TimePeriod gml:id="uuid.738e81ce-7c9b-471f-bf49-2d30efb712d5" frame="#ISO-8601">
                            <gml:beginPosition frame="#ISO-8601">2007-08-02T00:00:00Z</gml:beginPosition>
                            <gml:endPosition indeterminatePosition="unknown" frame="#ISO-8601"/>
                        </gml:TimePeriod>
                    </aixm:featureLifetime>
                    <aixm:type>D_OTHER</aixm:type>
                    <aixm:designator>EDFL</aixm:designator>
                    <aixm:name>FGDF</aixm:name>
                    <aixm:designatorICAO>NO</aixm:designatorICAO>
                    <aixm:controlType>CIVIL</aixm:controlType>
                    <aixm:geometryComponent>
                        <aixm:AirspaceGeometryComponent gml:id="uuid.ab5ac3fb-bf03-4128-86df-35061368215c">
                            <aixm:theAirspaceVolume>
                                <aixm:AirspaceVolume gml:id="uuid.944b396c-8f92-45d3-a14c-1d02d2d39c87">
                                    <aixm:upperLimit uom="FL">40</aixm:upperLimit>
                                    <aixm:upperLimitReference>STD</aixm:upperLimitReference>
                                    <aixm:lowerLimit uom="FT">3510</aixm:lowerLimit>
                                    <aixm:lowerLimitReference>MSL</aixm:lowerLimitReference>
                                    <aixm:horizontalProjection>
                                        <aixm:Surface xsi:type="aixm:ElevatedSurfaceType" gml:id="uuid.17a135e0-8e6d-4086-aae1-8ee552b4cd00">
                                            <gml:patches>
                                                <gml:PolygonPatch interpolation="planar">
                                                    <gml:exterior>
                                                        <gml:Ring>
                                                            <gml:curveMember xlink:type="simple" owns="false">
                                                                <gml:Curve xsi:type="aixm:CurveType" srsName="urn:ogc:def:crs:EPSG::4326"
 gml:id="uuid.5faf7d1e-e0f6-484c-a4cc-965341d5a780">
                                                                    <gml:segments>
                                                                        <gml:ArcByCenterPoint interpolation="circularArcCenterPointWithRadius" numArc="1"
 numDerivativesAtStart="0" numDerivativesAtEnd="0"
 numDerivativeInterior="0">
                                                                            <gml:pos>51.165 4.181289</gml:pos>
                                                                            <gml:radius uom="[nmi_i]">40.0</gml:radius>
                                                                            <gml:startAngle uom="deg">121.451258936276643</gml:startAngle>
                                                                            <gml:endAngle uom="deg">134.6012057585252537</gml:endAngle>
                                                                        </gml:ArcByCenterPoint>
                                                                        <gml:GeodesicString interpolation="geodesic" numDerivativesAtStart="0" numDerivativesAtEnd="0"
 numDerivativeInterior="0">
                                                                            <gml:posList>50.69389 4.9312417 50.7122806 4.86556 50.7129444 4.9175</gml:posList>
                                                                        </gml:GeodesicString>
                                                                        <gml:ArcByCenterPoint interpolation="circularArcCenterPointWithRadius" numArc="1"
 numDerivativesAtStart="0" numDerivativesAtEnd="0"
 numDerivativeInterior="0">
                                                                            <gml:pos>50.78167 4.95778</gml:pos>
                                                                            <gml:radius uom="[nmi_i]">1.7</gml:radius>
                                                                            <gml:startAngle uom="deg">296.6484324915256</gml:startAngle>
                                                                            <gml:endAngle uom="deg">357.5095314492478</gml:endAngle>
                                                                        </gml:ArcByCenterPoint>
                                                                        <gml:GeodesicString interpolation="geodesic" numDerivativesAtStart="0" numDerivativesAtEnd="0"
> numDerivativeInterior="0">
                                                                            <gml:posList>50.81 4.95583 50.1281333 5.08472</gml:posList>
                                                                        </gml:GeodesicString>
                                                                    </gml:segments>
                                                                </gml:Curve>
                                                            </gml:curveMember>
                                                        </gml:Ring>
                                                    </gml:exterior>
                                                </gml:PolygonPatch>
                                            </gml:patches>
                                        </aixm:Surface>
                                    </aixm:horizontalProjection>
                            </aixm:AirspaceVolume>
                            </aixm:theAirspaceVolume>
                        </aixm:AirspaceGeometryComponent>
                    </aixm:geometryComponent>
                </aixm:AirspaceTimeSlice>           </aixm:timeSlice>       </aixm:Airspace>    </hasMember>    </AIXMBasicMessage>

回答1:

-- edited in response to clarifications --

I would suggest you try it this way:

<xsl:variable name="count-points">
    <xsl:for-each select="/message:AIXMBasicMessage/message:hasMember/aixm:Airspace/aixm:timeSlice/aixm:AirspaceTimeSlice//gml:pos">
        <n>1</n>
    </xsl:for-each>
    <xsl:for-each select="/message:AIXMBasicMessage/message:hasMember/aixm:Airspace/aixm:timeSlice/aixm:AirspaceTimeSlice//gml:posList">
        <xsl:variable name="spaces" select="translate(normalize-space(.), '0123.456789.', '')" />
        <n>
            <xsl:value-of select="ceiling(string-length($spaces) div 2)"/>
        </n>
    </xsl:for-each>
</xsl:variable>     

<p>Count of Points: <xsl:value-of select="sum(exsl:node-set($count-points)/n)"/></p>

This would come after the </table> closing tag. You will also need to add the following namespace declaration to your stylesheet element:

xmlns:exsl="http://exslt.org/common"

Note: your attempt to count the div elements in the output cannot work; XSLT processes the input XML and does not have access to its own output.


Added:

Here's an alternative approach to the entire problem that eliminates the need to process the nodes twice - once to extract the points, and once to count them. And the output looks nicer, too.

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:message="http://www.aixm.aero/schema/5.1/message"
xmlns:gml="http://www.opengis.net/gml/3.2" 
xmlns:aixm="http://www.aixm.aero/schema/5.1" 
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="message gml aixm exsl">
<xsl:output method="html" encoding="UTF-8"/>

<xsl:template match="/">
    <!-- first-pass -->
    <xsl:variable name="points">
        <xsl:for-each select="message:AIXMBasicMessage/message:hasMember/aixm:Airspace/aixm:timeSlice/aixm:AirspaceTimeSlice">
            <group designator="{aixm:designator}">
                <xsl:for-each select=".//gml:pos">
                    <point>
                        <xsl:value-of select="."/>
                    </point>
                </xsl:for-each>
                <xsl:for-each select=".//gml:posList">
                    <xsl:call-template name="split"/>
                </xsl:for-each>
            </group>
        </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="points-set" select="exsl:node-set($points)" />
    <!-- output -->
    <html>
        <body>
            <h2>Designated Points</h2>
            <table border="1">
                <tr bgcolor="#9acd32">
                    <th>Designator</th>
                    <th>Coordinates</th>
                </tr>
                <xsl:for-each select="$points-set/group">
                    <tr>
                        <td rowspan="{count(point) + 1}">
                            <xsl:value-of select="@designator"/>
                        </td>
                    </tr>
                    <xsl:for-each select="point">
                        <tr>
                            <td>
                                <xsl:value-of select="."/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </xsl:for-each>
            </table>
            <p>Count of Points: <xsl:value-of select="count($points-set//point)"/></p>
        </body>
    </html>
</xsl:template>

<xsl:template name="split">
    <xsl:param name="text" select="normalize-space(.)"/>
    <xsl:if test="string($text)">
        <point>
            <xsl:value-of select="substring-before($text, ' ')"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="substring-before(substring-after(concat($text, ' '), ' '), ' ')"/>
        </point>
        <xsl:call-template name="split">
            <xsl:with-param name="text" select="substring-after(substring-after($text, ' '), ' ')"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>


</xsl:stylesheet>