首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用轴在xml中处理此变量

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

Stack Overflow用户
提问于 2010-08-31 07:45:34
回答 2查看 127关注 0票数 1

我需要从下面的xml中获取以下值。为了简洁起见,我把它剪下来了,它走了相当长的路。我需要提取:

name

  • ppm

  • rolling
  • 算子

ia我的xslt样式表,但我不知道如何正确地处理元素。

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-31 13:10:57

使用以下XPath表达式

代码语言:javascript
复制
/*/nr:RTPPMData/nr:NationalPage/nr:Operator/@name

上面的选项选择了name属性的nr:Operator

代码语言:javascript
复制
/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:PPM

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

代码语言:javascript
复制
/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:RollingPPM

上面的选项选择所有nr:RollingPPM元素。

确实注意到了总是试图避免,因为这是导致XPath表达式效率低下的潜在原因。

在下面的转换中说明了这一点

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
<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>

想要的,正确的结果产生

代码语言:javascript
复制
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>
票数 0
EN

Stack Overflow用户

发布于 2010-08-31 12:05:42

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

(在实践中,您可能不希望使用//搜索文档根的所有后代,而是处理相对于当前上下文节点的元素。)

代码语言:javascript
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3607005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档