You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过键获取子标签值的XSLT 1.0技术问题

XSLT 1.0 部分节点匹配失效排查与解决

问题场景

修改XSLT 1.0样式表时遇到异常:匹配同一父节点下的部分子节点/属性能正常返回值,另一部分却无输出,尽管它们的层级结构一致。

相关XML片段

l命名空间的XML片段

<Survey>
<SurveyHeader name="230222-CODE-TEST_1"/>
<Equipment>
<GPSReceiverDetails id="GPS_1" manufacturer="Leica Geosystems AG" model=" " serialNumber=" "/>
</Equipment>
<GPSSetup id="GPSSetupID_4" GPSReceiverDetailsID="GPS_1" antennaHeight="1.544000" stationName="GPS0001">
<GPSPosition pntRef="GPS0001" wgsLatitude="53.201424895062324" wgsLongitude="6.564586909979438" wgsHeight="42.733122">
<TargetPoint name="GPS0001" pntRef="GPS0001" >580062.951336 233674.586902 1.958197</TargetPoint>
<GPSQCInfoLevel2 covarianceXX="0.0000556906" covarianceXY="-0.0000044136" covarianceXZ="0.0000055377" covarianceYY="0.0000384521" covarianceYZ="0.0000021151" covarianceZZ="0.0000702572" RMS="1.000000" startTime="1361092385" stopTime="1361092385"/>
</GPSPosition>
</GPSSetup>
</Survey>

h命名空间的XML片段

<Survey name="230222-CODE-TEST_1" SurveyDuration="P0Y0M0DT0H4M4S">
<GPSSetup uniqueID="GPSSetupID_4">
<GPSPosition targetPntRef="GPS0001" tiltCompensated="true" verticalOffset="0.000000">
<GPSQuality HDOP="0.600000" GDOP="1.878829" PDOP="1.252996" VDOP="1.100000" TDOP="1.400000"/>
<SatelliteInfo GPSSatTracked="9" GPSSatUsed="9" GALILEOSatTracked="4" GALILEOSatUsed="4" GLONASSSatTracked="6" GLONASSSatUsed="5" BEIDOUSatTracked="12" BEIDOUSatUsed="12"/>
<RTKInfo referenceRef="RTCM-Ref 0028" networkSolution="true" networkType="i-MAX" dataFormat="RTCM v3" ipAddress="nl.nrtk.eu" port="7801" mountpoint="MSM_iMAX" insideRTKNetwork="Inside" GPUID="" numRefStationUsed="1" numRTKPositionUsed="0"/>
<GPSPositionVelocity velocityX="0.017137" velocityY="0.001990" velocityZ="-0.010346" linearVelocityUnit="linearUnitPerSecond">
<VelocityQuality Qxx="0.00073671" Qxy="-0.00014277" Qxz="0.00014533" Qyy="0.00039546" Qyz="-0.00000568" Qzz="0.00262066" MeanError="1.00000000"/>
</GPSPositionVelocity>
<TiltInfo tilt="2.09289226" tiltQuality="0.20604831" maxTilt="2.09289172" maxTiltQuality="0.20604831" tiltDirection="292.82119353" tiltDirectionQuality="6.73585423" antennaHeading="126.49655262" antennaHeadingQuality="1.56022984">
<AttitudeQuaternion w="0.545783971615954" x="0.014694157165700" y="-0.007365463010343" z="-0.837764697290921">
<QuaternionQuality Qxx="0.0000105379" Qxy="0.0000002941" Qxz="-0.0000101108" Qyy="0.0000108098" Qyz="0.0000066687" Qzz="0.0008271720" MeanError="63.6619772368"/>
</AttitudeQuaternion>
<AngularVelocity velocityX="-3.1438191155" velocityY="1.5740678692" velocityZ="-1.4080006143" AngularVelocityType="angularUnitPerSecond">
<VelocityQuality Qxx="0.0000001490" Qxy="0.0000002552" Qxz="-0.0000000169" Qyy="0.0000005309" Qyz="0.0000000154" Qzz="0.0000003482" MeanError="63.6619772368"/>
</AngularVelocity>
<LeverArm deltaNorth="0.001000" deltaEast="0.000280" deltaUp="-1.643910"/>
</TiltInfo>
</GPSPosition>
</GPSSetup>
</Survey>

现有XSLT代码片段

<xsl:key name="KeyGPSPositionH" match="h:GPSPosition" use="@targetPntRef" />

<xsl:template match="l:GPSPosition">
  <xsl:variable name="gpsSetupH" select="key('KeyGPSPositionH',@pntRef)" />
  
  <!-- 以下调用出现差异 -->
  <xsl:value-of select="$gpsSetupH/h:GPSQuality/@HDOP" /> <!-- 正常返回值 -->
  <xsl:value-of select="$gpsSetupH/h:SatelliteInfo/@GPSSatTracked" /> <!-- 正常返回值 -->
  <xsl:value-of select="$gpsSetupH/h:TiltInfo/@tiltQuality" /> <!-- 无返回值 -->
  <xsl:value-of select="$gpsSetupH/@verticalOffset" /> <!-- 无返回值 -->
</xsl:template>

问题分析与解决

核心原因

大概率是命名空间绑定不一致:

  • 能获取GPSQuality和SatelliteInfo的值,说明h:前缀已正确绑定到对应命名空间且key匹配正常;但TiltInfo和verticalOffset失效,意味着这部分节点/属性的命名空间未与h:前缀正确关联。

排查与修复步骤

  1. 核对命名空间声明:检查XSLT头部的命名空间绑定,确保h:前缀对应的URL与XML文档中Survey、GPSPosition、TiltInfo等节点实际所属的命名空间完全一致(包括大小写、URL拼写)。
  2. 调试节点匹配结果:添加调试代码确认$gpsSetupH是否指向预期节点:
    <!-- 调试用:输出匹配节点的名称和targetPntRef属性 -->
    <xsl:value-of select="name($gpsSetupH)" />
    <xsl:value-of select="$gpsSetupH/@targetPntRef" />
    
  3. 修正错误路径:你尝试的$gpsSetupH/h:GPSPosition/h:TiltInfo是冗余错误路径——$gpsSetupH本身就是h:GPSPosition节点,无需再追加h:GPSPosition层级。

正确调用方式

确认命名空间绑定无误后,保持原有简洁路径即可:

<xsl:value-of select="$gpsSetupH/h:TiltInfo/@tiltQuality" />
<xsl:value-of select="$gpsSetupH/@verticalOffset" />

内容的提问来源于stack exchange,提问作者Lars81

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 07:35:04