我需要从下面的xml中获取以下值。为了简洁起见,我把它剪下来了,它走了相当长的路。我需要提取:
name
ia我的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>^<5 mins; *<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 'on time'. </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>发布于 2010-08-31 13:10:57
使用以下XPath表达式
/*/nr:RTPPMData/nr:NationalPage/nr:Operator/@name上面的选项选择了name属性的nr:Operator。
/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:PPM以上选择所有nr:PPM元素。
/*/nr:RTPPMData/nr:NationalPage/nr:Sector/nr:SectorPPM/nr:RollingPPM上面的选项选择所有nr:RollingPPM元素。
确实注意到了:总是试图避免,因为这是导致XPath表达式效率低下的潜在原因。
在下面的转换中说明了这一点
<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>
</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文档(基于提供的文本,但格式良好):
<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>^<5 mins; *<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 'on time'. </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>想要的,正确的结果产生
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>发布于 2010-08-31 12:05:42
下面的样式表演示了基本用法,这可能对您有所帮助。请记住,您需要声明您正在使用的名称空间前缀,如下面的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>https://stackoverflow.com/questions/3607005
复制相似问题