计算节点XSLT

时间:2016-01-19 13:48:29

标签: xml xslt sum counting

  

我需要帮助计算项目中的空格gml:pos和gml:posList和>汇总空格数然后div 2.我需要gml:pos和gml:posList / 2中所有whitespece的结果编号   我使用XSLT 1.0

     

感谢您的帮助

这是我的尝试

    <xsl:template match="/" name="count">
  <xsl:for-each select="//gml:posList|//gml:pos">
  <xsl:value-of select="sum(string-length(.)-string-length(translate(.,' ','')))"/>
  </xsl:for-each>
  </xsl:template>

这是输入:

 <?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>

2 个答案:

答案 0 :(得分:3)

您需要做的是将所有节点连接在一个变量中,如下所示:

<xsl:variable name="allPoints">
   <xsl:for-each select="//gml:posList|//gml:pos">
    <xsl:value-of select="normalize-space()" />
  </xsl:for-each>
</xsl:variable>

然后,您可以将当前表达式应用于此变量,以获取空格的总数

<xsl:value-of select="sum(string-length($allPoints)-string-length(translate($allPoints,' ','')))"/>

试试这个XSLT作为例子

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

<xsl:template match="/">
  <xsl:call-template name="count" />
</xsl:template>

<xsl:template name="count">
  <xsl:variable name="allPoints">
    <xsl:for-each select="//gml:posList|//gml:pos">
      <xsl:value-of select="normalize-space()" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:value-of select="sum(string-length($allPoints)-string-length(translate($allPoints,' ','')))"/>
  </xsl:template> 
</xsl:stylesheet>

答案 1 :(得分:1)

sum()用于节点集,而不是用于求和标量值。例如,如果您有一组节点并且想要添加其(数字)内容,则使用sum()。您的模板应该调整为:

<xsl:template match="/" >
  <xsl:for-each select="//gml:pos | //gml:posList">
    <!--<xsl:value-of select="local-name()" /> with content <xsl:value-of select="." /> has --> 
    <xsl:value-of select="string-length(.) - string-length(translate(.,' ',''))"/> <!-- whitespace characters  -->
  </xsl:for-each>
</xsl:template>