从父节点子属性获取值

时间:2016-02-20 17:16:22

标签: xml xslt xslt-1.0

我对XLST很新。我已经尝试了几周将xml文件转换为纯文本文件。我想检查一个特定的属性值,并根据父节点子节点中的以下属性打印出一个值。

我尝试了不同的方法,我完全感到困惑,代码可能看起来很混乱。

以下是我正在尝试使用的xml的摘录:

func SearchC(){
    let pathMC = NSBundle.mainBundle().pathForResource("myPlistArray", ofType: "plist")
    let dictMC = NSDictionary(contentsOfFile: pathMC!)
    myPlistArray = dictMC!["Questions"]!.mutableCopy() as? Array

    if let questionArray = myPlistArray as? [[String:AnyObject]] {
        let answersC = questionArray.filter({
            if let correct = $0["CorrectAnswer"] as? String {
                return correct.uppercaseString == "C"
            }
            return false
        })
        print("\(answersC)")
    }
}

我现在的xls:

func SearchYES(){
    let pathMC = NSBundle.mainBundle().pathForResource("myPlistArray", ofType: "plist")
    let dictMC = NSDictionary(contentsOfFile: pathMC!)
    myPlistArray = dictMC!["Questions"]!.mutableCopy() as? Array

    if let questionArray = myPlistArray as? [[String:AnyObject]] {
        let isFavorite = questionArray.filter({
            if let favorite = $0["isFavorite"] as? String {
                return favorite.uppercaseString == "YES"
            }
            return false
        })
        print("\(isFavorite)")
    }
}

所以这部分我无法开始工作:

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

简而言之,我想检查下一个Point / PointCode / @ codeLinework的值并打印出一个值。

1 个答案:

答案 0 :(得分:0)

如果你能发布预期的结果会更好,所以我们可以提供更准确的答案。即便如此,我还是试着根据您的意见,并通过微小的改动创建了这个:

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

我更改了Point和PointCode的选择器,因为它们不正确,我稍微改变了你试图用来获得以下兄弟的XPath表达式。

这是我针对您的XML运行时的输出:

RKO,41,EBS,30,