我对XLST来说很陌生。我已经尝试了几个星期将xml文件转换为纯文本文件。我想检查特定的属性值,并根据父子节点中的以下属性打印出一个值。
我尝试了不同的方法,但我完全困惑,代码可能看起来很混乱。
以下是我尝试使用的 xml 的摘录:
<?xml version="1.0" encoding="utf-8"?>
<LandXML xmlns="http://www.landxml.org/schema/LandXML-1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.landxml.org/schema/LandXML-1.2 http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd" version="1.2" date="2016-02-11" time="13:38:06" readOnly="false" language="English">
<HexagonLandXML xmlns="http://xml.hexagon.com/schema/HeXML-1.7" xsi:schemaLocation="http://xml.hexagon.com/schema/HeXML-1.7 http://xml.hexagon.com/schema/HeXML-1.7.xsd" xmlns:landxml="http://www.landxml.org/schema/LandXML-1.2" averagingMode="Average" averagingMethod="Weighted" averagingPosLimit="0.050000" averagingHgtLimit="0.075000">
<Point uniqueID="100" class="measured" subclass="GPS phase" lineworkFlag="FORT" averageUsage="true" applicationNumber="11">
<Coordinates originalCoordSysKind="Ellipsoidal" originalGeodeticDatumKind="WGS84" originalHeightKind="ellipsoidal">
<WGS84>
<Cartesian x="3374756.763053" y="893574.649632" z="5320249.612556"/>
<Geodetic lat="56.904101405681359" lon="14.830552433162786" hghtE="206.861483" hghthO="174.187582"/>
</WGS84>
<Local>
<Cartesian x="3374756.763053" y="893574.649632" z="5320249.612556"/>
<Geodetic lat="56.904101406543710" lon="14.830552433162786" hghtE="206.861556" hghthO="174.187583"/>
<Grid e="139675.834002" n="6309247.502715" hghtE="206.861556" hghthO="174.187583"/>
</Local>
</Coordinates>
<PointCode code="RKO" codeDesc="" codeGroup="TRANSPORT" codeLinework="open line"></PointCode>
<PointQuality Qxx="0.0003475127" Qxy="0.0000634192" Qxz="-0.0003087937" Qyy="0.0002335805" Qyz="-0.0001324870" Qzz="0.0014808670" MeanError="1.156631" CQ3D="0.052521" CQPos="0.027882" CQHeight="0.044509"/>
</Point>
<Point uniqueID="101" class="measured" subclass="GPS phase" averageUsage="true" applicationNumber="11">
<Coordinates originalCoordSysKind="Ellipsoidal" originalGeodeticDatumKind="WGS84" originalHeightKind="ellipsoidal">
<WGS84>
<Cartesian x="3374779.920766" y="893569.513712" z="5320236.614863"/>
<Geodetic lat="56.903879154810298" lon="14.830373667852458" hghtE="207.478635" hghthO="174.804021"/>
</WGS84>
<Local>
<Cartesian x="3374779.920766" y="893569.513712" z="5320236.614863"/>
<Geodetic lat="56.903879155672669" lon="14.830373667852458" hghtE="207.478708" hghthO="174.804022"/>
<Grid e="139664.880750" n="6309222.780312" hghtE="207.478708" hghthO="174.804022"/>
</Local>
</Coordinates>
<PointCode code="EBS" codeDesc="Belysningsstolpe" codeGroup="ELLEDNING" codeLinework="none">
<Attribute name="Attribut 1" value="-5"/>
</PointCode>
<PointQuality Qxx="0.0001718767" Qxy="-0.0000094370" Qxz="0.0000030377" Qyy="0.0000956758" Qyz="-0.0000386372" Qzz="0.0007723176" MeanError="0.630108" CQ3D="0.020319" CQPos="0.010307" CQHeight="0.017511"/>
</Point>
</HexagonLandXML>
</LandXML>
和我目前的 xls:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:landxml="http://www.landxml.org/schema/LandXML-1.2" xmlns:hexagon="http://xml.hexagon.com/schema/HeXML-1.7" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="UTF-16" indent="no" omit-xml-declaration="yes"/>
<xsl:variable name="XML" select="/"/>
<xsl:variable name="fileExt" select="'txt'"/>
<xsl:variable name="fileDesc" select="'Output pointno, easting, northing, and ortho height to three decimal places. + handling of linecodes e.g. (40,41)'"/>
<xsl:template match="/">
<xsl:for-each select="$XML">
<xsl:for-each select="landxml:LandXML">
<xsl:for-each select="hexagon:HexagonLandXML">
<xsl:for-each select="hexagon:Point">
<xsl:for-each select="@uniqueID">
<xsl:value-of select="string(.)"/>
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:for-each select="hexagon:Coordinates">
<xsl:for-each select="hexagon:Local">
<xsl:for-each select="hexagon:Grid">
<xsl:for-each select="@e">
<xsl:value-of select='format-number(., "########.000")' />
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:for-each select="@n">
<xsl:value-of select='format-number(., "########.000")' />
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:for-each select="@hghthO">
<xsl:value-of select='format-number(., "########.000")' />
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
<xsl:for-each select="hexagon:PointCode">
<xsl:value-of select="string(@code)"/>
<xsl:text>,</xsl:text>
<xsl:if test="@codeLinework='open line'" >
<xsl:choose>
<xsl:when test="../@lineworkFlag='START LINJE'">
<xsl:text>40</xsl:text>
<xsl:text>,</xsl:text>
</xsl:when>
<xsl:when test="../following-sibling::*[1]/PointCode[@codeLinework='none']">
<xsl:text>41</xsl:text>
<xsl:text>,</xsl:text>
</xsl:when>
<!-- <xsl:when test="../@codeLinework">
<xsl:text> </xsl:text>
<xsl:text>,</xsl:text>
</xsl:when>-->
</xsl:choose>
</xsl:if>
<xsl:if test="@codeLinework='none'">
<xsl:text>30</xsl:text>
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
所以这是我无法工作的这一部分:
<xsl:when test="../following-sibling::*[1]/PointCode[@codeLinework='none']">
<xsl:text>41</xsl:text>
<xsl:text>,</xsl:text>
</xsl:when>
简而言之,我想检查下一个点/点代码/@codeLinework的值并打印出一个值。
如果您能发布预期的结果会更好,这样我们就可以提供更准确的答案。即便如此,我还是尝试根据您的评论进行修改,并进行了微小的更改,创建了以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:landxml="http://www.landxml.org/schema/LandXML-1.2"
xmlns:hexxml="http://xml.hexagon.com/schema/HeXML-1.7"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<xsl:for-each select="landxml:LandXML/hexxml:HexagonLandXML/hexxml:Point">
<xsl:for-each select="./hexxml:PointCode">
<xsl:value-of select="string(@code)"/>
<xsl:text>,</xsl:text>
<xsl:if test="@codeLinework='open line'">
<xsl:choose>
<xsl:when test="../@lineworkFlag='START LINJE'">
<xsl:text>40</xsl:text>
<xsl:text>,</xsl:text>
</xsl:when>
<xsl:when test="../following-sibling::*[1]/hexxml:PointCode[@codeLinework='none']">
<xsl:text>41</xsl:text>
<xsl:text>,</xsl:text>
</xsl:when>
<xsl:when test="../@codeLinework">
<xsl:text> </xsl:text>
<xsl:text>,</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:if>
<xsl:if test="@codeLinework='none'">
<xsl:text>30</xsl:text>
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我更改了点和点代码的选择器,因为它们不正确,并且我稍微更改了您尝试用于获取以下同级的 XPath 表达式。
这是我针对您的 XML 运行时的输出:
RKO,41,EBS,30,