开发者

how to address this variable in xml using axes

I need to get the following values out of the xml below. I've snipped it for brevity, it goes on quite some way. I need to extract:

  • operator name
  • ppm
  • rolling ppm

ia my xslt stylesheet, but i dont know how to correctly address the elements.

<?xml version="1.0" encoding="UTF-8"?>
<nr:RTPPMDataMsgV1 owner="Network Rail"开发者_运维知识库 timestamp="2010-08-27T13:41:04.0Z" classification="public" xsi:schemaLocation="http://xml.networkrail.co.uk/ns/2007/NR rtppm_messaging_v1.17.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR">
<msg:Sender application="RTPPM3" organisation="String"/>
<msg:Publication>
<msg:TopicID>RTPPM3/InternalPPM</msg:TopicID>
</msg:Publication>
<nr:RTPPMData snapshotTStamp="2010-08-27T13:41:02.0Z">
<nr:SystemMsg/>
<nr:RAGThresholds type="TOC" medium="87" good="92"/>
<nr:RAGThresholds type="FOC" medium="70" good="80"/>
<nr:RAGThresholds type="PPT" medium="85" good="91"/>
<nr:WebPPMLink>http://connect/Performance/PPM/PPMGuide.doc x</nr:WebPPMLink>
<nr:PPT rag="G" ragDisplayFlag="Y">93</nr:PPT>
<nr:NationalPage WebDisplayPeriod="60">
  <nr:WebFixedMsg1>^&lt;5 mins; *&lt;10 mins</nr:WebFixedMsg1>
    <nr:WebFixedMsg2>The Public Performance Measure shows the performance of trains against the timetable, measured as the percentage of trains arriving at destination &apos;on time&apos;. </nr:WebFixedMsg2>
    <nr:WebMsgOfMoment>FGW:- TCF Reading. East Coast: Unit failure Fitzwilliam.</nr:WebMsgOfMoment>
    <nr:StaleFlag>N</nr:StaleFlag>
    <nr:NationalPPM>
    <nr:Total>8869</nr:Total>
    <nr:OnTime>8393</nr:OnTime>
    <nr:Late>476</nr:Late>
    <nr:CancelVeryLate>85</nr:CancelVeryLate>
    <nr:PPM rag="G" ragDisplayFlag="N">94</nr:PPM>
    <nr:RollingPPM trendInd="-" rag="G">93</nr:RollingPPM>
  </nr:NationalPPM>
  <nr:Sector sectorDesc="London and South East" sectorCode="LSE">
       <nr:SectorPPM>
        <nr:Total>4868</nr:Total>
        <nr:OnTime>4613</nr:OnTime>
        <nr:Late>255</nr:Late>
        <nr:CancelVeryLate>45</nr:CancelVeryLate>
        <nr:PPM rag="G">94</nr:PPM>
        <nr:RollingPPM trendInd="-" rag="G">93</nr:RollingPPM>
        </nr:SectorPPM>
       </nr:Sector><nr:Sector sectorDesc="Long Distance" sectorCode="LD">
       <nr:SectorPPM>
        <nr:Total>587</nr:Total>
        <nr:OnTime>541</nr:OnTime>
        <nr:Late>46</nr:Late>
        <nr:CancelVeryLate>14</nr:CancelVeryLate>
        <nr:PPM rag="G">92</nr:PPM>
        <nr:RollingPPM trendInd="-" rag="A">89</nr:RollingPPM>
        </nr:SectorPPM>
       </nr:Sector><nr:Sector sectorDesc="Regional" sectorCode="REG">
       <nr:SectorPPM>
        <nr:Total>2485</nr:Total>
        <nr:OnTime>2350</nr:OnTime>
        <nr:Late>135</nr:Late>
        <nr:CancelVeryLate>24</nr:CancelVeryLate>
        <nr:PPM rag="G">94</nr:PPM>
        <nr:RollingPPM trendInd="-" rag="G">93</nr:RollingPPM>
        </nr:SectorPPM>
       </nr:Sector><nr:Sector sectorDesc="Scotland" sectorCode="SCO">
       <nr:SectorPPM>
        <nr:Total>931</nr:Total>
        <nr:OnTime>890</nr:OnTime>
        <nr:Late>41</nr:Late>
        <nr:CancelVeryLate>2</nr:CancelVeryLate>
        <nr:PPM rag="G">95</nr:PPM>
        <nr:RollingPPM trendInd="=" rag="G">95</nr:RollingPPM>
        </nr:SectorPPM>
       </nr:Sector><nr:Operator code="61" keySymbol="*" name="East Coast">
         <nr:Total>45</nr:Total>
         <nr:PPM rag="R">64</nr:PPM>
         <nr:RollingPPM trendInd="-" displayFlag="Y" rag="R">60</nr:RollingPPM>
         </nr:Operator>


Use the following XPath expressions:

/*/nr:RTPPMData/nr:NationalPage/nr:Operator/@name

the above selects the name attribute of the nr:Operator .

/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:PPM

the above selects all nr:PPM elements.

/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:RollingPPM

the above selects all nr:RollingPPM elements.

Do note: Always try to avoid the // abbreviation because it is potential cause for very inefficient XPath expressions.

This is illustrated in the following transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR"
 >

 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:template match="/">
  <xsl:value-of select="/*/nr:RTPPMData/nr:NationalPage/nr:Operator/@name"/>
  <xsl:text>&#xA;</xsl:text>
  <xsl:copy-of 
    select="/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:PPM"/>
  <xsl:copy-of 
    select="/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:RollingPPM"/>
 </xsl:template>
</xsl:stylesheet>

When applied to the following XML document (based on the provided text, but made well-formed):

<nr:RTPPMDataMsgV1 owner="Network Rail" timestamp="2010-08-27T13:41:04.0Z" classification="public" xsi:schemaLocation="http://xml.networkrail.co.uk/ns/2007/NR rtppm_messaging_v1.17.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR">
    <msg:Sender application="RTPPM3" organisation="String"/>
    <msg:Publication>
        <msg:TopicID>RTPPM3/InternalPPM</msg:TopicID>
    </msg:Publication>
    <nr:RTPPMData snapshotTStamp="2010-08-27T13:41:02.0Z">
        <nr:SystemMsg/>
        <nr:RAGThresholds type="TOC" medium="87" good="92"/>
        <nr:RAGThresholds type="FOC" medium="70" good="80"/>
        <nr:RAGThresholds type="PPT" medium="85" good="91"/>
        <nr:WebPPMLink>http://connect/Performance/PPM/PPMGuide.doc x</nr:WebPPMLink>
        <nr:PPT rag="G" ragDisplayFlag="Y">93</nr:PPT>
        <nr:NationalPage WebDisplayPeriod="60">
            <nr:WebFixedMsg1>^&lt;5 mins; *&lt;10 mins</nr:WebFixedMsg1>
            <nr:WebFixedMsg2>The Public Performance Measure shows the performance of trains against the timetable, measured as the percentage of trains arriving at destination &apos;on time&apos;. </nr:WebFixedMsg2>
            <nr:WebMsgOfMoment>FGW:- TCF Reading. East Coast: Unit failure Fitzwilliam.</nr:WebMsgOfMoment>
            <nr:StaleFlag>N</nr:StaleFlag>
            <nr:NationalPPM>
                <nr:Total>8869</nr:Total>
                <nr:OnTime>8393</nr:OnTime>
                <nr:Late>476</nr:Late>
                <nr:CancelVeryLate>85</nr:CancelVeryLate>
                <nr:PPM rag="G" ragDisplayFlag="N">94</nr:PPM>
                <nr:RollingPPM trendInd="-" rag="G">93</nr:RollingPPM>
            </nr:NationalPPM>
            <nr:Sector sectorDesc="London and South East" sectorCode="LSE">
                <nr:SectorPPM>
                    <nr:Total>4868</nr:Total>
                    <nr:OnTime>4613</nr:OnTime>
                    <nr:Late>255</nr:Late>
                    <nr:CancelVeryLate>45</nr:CancelVeryLate>
                    <nr:PPM rag="G">94</nr:PPM>
                    <nr:RollingPPM trendInd="-" rag="G">93</nr:RollingPPM>
                </nr:SectorPPM>
            </nr:Sector>
            <nr:Sector sectorDesc="Long Distance" sectorCode="LD">
                <nr:SectorPPM>
                    <nr:Total>587</nr:Total>
                    <nr:OnTime>541</nr:OnTime>
                    <nr:Late>46</nr:Late>
                    <nr:CancelVeryLate>14</nr:CancelVeryLate>
                    <nr:PPM rag="G">92</nr:PPM>
                    <nr:RollingPPM trendInd="-" rag="A">89</nr:RollingPPM>
                </nr:SectorPPM>
            </nr:Sector>
            <nr:Sector sectorDesc="Regional" sectorCode="REG">
                <nr:SectorPPM>
                    <nr:Total>2485</nr:Total>
                    <nr:OnTime>2350</nr:OnTime>
                    <nr:Late>135</nr:Late>
                    <nr:CancelVeryLate>24</nr:CancelVeryLate>
                    <nr:PPM rag="G">94</nr:PPM>
                    <nr:RollingPPM trendInd="-" rag="G">93</nr:RollingPPM>
                </nr:SectorPPM>
            </nr:Sector>
            <nr:Sector sectorDesc="Scotland" sectorCode="SCO">
                <nr:SectorPPM>
                    <nr:Total>931</nr:Total>
                    <nr:OnTime>890</nr:OnTime>
                    <nr:Late>41</nr:Late>
                    <nr:CancelVeryLate>2</nr:CancelVeryLate>
                    <nr:PPM rag="G">95</nr:PPM>
                    <nr:RollingPPM trendInd="=" rag="G">95</nr:RollingPPM>
                </nr:SectorPPM>
            </nr:Sector>
            <nr:Operator code="61" keySymbol="*" name="East Coast">
                <nr:Total>45</nr:Total>
                <nr:PPM rag="R">64</nr:PPM>
                <nr:RollingPPM trendInd="-" displayFlag="Y" rag="R">60</nr:RollingPPM>
            </nr:Operator>
        </nr:NationalPage>
    </nr:RTPPMData>
</nr:RTPPMDataMsgV1>

the wanted, correct result is produced:

East Coast
<nr:PPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" rag="G">94</nr:PPM>
<nr:PPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" rag="G">92</nr:PPM>
<nr:PPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" rag="G">94</nr:PPM>
<nr:PPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" rag="G">95</nr:PPM>
<nr:RollingPPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" trendInd="-" rag="G">93</nr:RollingPPM>
<nr:RollingPPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" trendInd="-" rag="A">89</nr:RollingPPM>
<nr:RollingPPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" trendInd="-" rag="G">93</nr:RollingPPM>
<nr:RollingPPM xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:msg="http://xml.networkrail.co.uk/ns/2007/EAI" trendInd="=" rag="G">95</nr:RollingPPM>


The stylesheet below demonstrates basic usage, which might help you. Remember that you need to declare the namespace prefixes you are using, like nr: below.

(In practice, you probably don't want to use // to search all the descendants of the document root, instead you would address elements relative to the current context node.)

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:nr="http://xml.networkrail.co.uk/ns/2007/NR">
  <xsl:output method="text"/>
  <xsl:template match="/">

    operator name: 
    <xsl:value-of select="//nr:Operator/@name"/>

    ppm:
    <xsl:value-of select="//nr:PPM"/>

    rolling ppm:
    <xsl:value-of select="//nr:RollingPPM"/>

  </xsl:template>
</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜