如何使用axis在xml中处理此变量

时间:2010-08-31 07:45:35

标签: xml xslt

我需要从下面的xml中获取以下值。我为了简洁而剪了它,它在某种程度上持续下去。我需要提取:

  • 运营商名称
  • ppm的
  • 滚动ppm

我的xslt样式表,但我不知道如何正确地解决这些元素。

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

2 个答案:

答案 0 :(得分:0)

下面的样式表演示了基本用法,可能会对您有所帮助。请记住,您需要声明正在使用的名称空间前缀,如下面的nr:

(实际上,您可能不希望使用//来搜索文档根目录的所有后代,而是相对于当前上下文节点寻址元素。)

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

答案 1 :(得分:0)

使用以下XPath表达式

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

以上选择name的{​​{1}}属性。

nr:Operator

以上选择所有nr:PPM元素。

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

以上选择所有nr:RollingPPM元素。

请注意始终尽量避免使用/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:RollingPPM 缩写,因为这可能导致效率非常低的XPath表达式。

以下转换说明了这一点

//

应用于以下XML文档(基于提供的文本,但格式正确):

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

产生了想要的正确结果

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