使用xslt我该如何解决XML文件中的这个特定元素?

问题描述:

以前从来没有为XML编码,完全失去了各种引脚,这些引脚都告诉我做一些不同的事情。这可能意味着你可以采用不同的方式,但它们都显示单节点的情况,但事实并非如此。使用xslt我该如何解决XML文件中的这个特定元素?

我试过各种方法挑出WebDisplayPeriod但我相信我没有实际针对该权利的属性。请爱人给我一个正确的方向推。任何关于XSLT更高级使用的可读引用都会被赞赏,因为w3schools是垃圾;)。

我有以下XML

<?xml version="1.0" encoding="UTF-8"?> 
<nr:RTPPMDataMsgV1 owner="Network Rail" timestamp="2010-08-05T10:27: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-05T10:27: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>EC: 1S02 train defect at Balne.</nr:WebMsgOfMoment> 
    <nr:StaleFlag>N</nr:StaleFlag> 
    <nr:NationalPPM> 
    <nr:Total>5079</nr:Total> 
    <nr:OnTime>4842</nr:OnTime> 
    <nr:Late>237</nr:Late> 
    <nr:CancelVeryLate>47</nr:CancelVeryLate> 
    <nr:PPM rag="G" ragDisplayFlag="N">95</nr:PPM> 
    <nr:RollingPPM trendInd="-" rag="G">94</nr:RollingPPM> 
    </nr:NationalPPM> 
...... 

它的推移和。但是我感兴趣的是位于上面的部分。

我使用下面的XSLT把它变成一个清晰的网页:

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xsi:schemaLocation="http://xml.networkrail.co.uk/ns/2007/NR rtppm_messaging_v1.6.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" > 
<xsl:output method="html" omit-xml-declaration="yes" indent="yes" /> 

<xsl:param name="mode">TOCPage</xsl:param> 
<xsl:param name="location">internal-connect</xsl:param> 
<xsl:param name="table1Count">11</xsl:param> 
<xsl:param name="table2Count">23</xsl:param> 

    <!-- Root template--> 
    <xsl:template match="/nr:RTPPMDataMsgV1/nr:RTPPMData"> 
     <xsl:element name="{nr:NationalPage}"> 
      <xsl:attribute name="{WebDisplayPeriod}" > 
       <xsl:value-of select="."/> 
      </xsl:attribute> 
     </xsl:element> 
<html> 
<head> 
<meta http-equiv="content-type" CONTENT="text/css" /> 
<meta http-equiv="expires" CONTENT="-1" /> 
<meta http-equiv="pragma" CONTENT="no-cache" /> 
<meta http-equiv="cache-control" CONTENT="no-cache" /> 
<meta http-equiv="refresh" name="refresh" content="{$WebDisplayPeriod}" /> 
<title>EC RTPPM Feed</title> 
<link rel="stylesheet" type="text/css" href="stylesheet.css" /> 
<link rel="stylesheet" type="text/css" href="xslstylesheet.css" /> 
</head> 

<body> 
<p>Hello</p> 
     <p>Hello</p> 
</body> 
</html> 
</xsl:template > 
</xsl:stylesheet> 

和你已猜到了,它永远不会拿起WebDisplayPeriod。

尝试它是这样的:

<xsl:template match="/nr:RTPPMDataMsgV1/nr:RTPPMData"> 
    <xsl:variable name="WebDisplayPeriod" select="./nr:NationalPage/@WebDisplayPeriod" /> 

<html> 
<head> 
<meta http-equiv="content-type" CONTENT="text/css" /> 
<meta http-equiv="expires" CONTENT="-1" /> 
<meta http-equiv="pragma" CONTENT="no-cache" /> 
<meta http-equiv="cache-control" CONTENT="no-cache" /> 
<meta http-equiv="refresh" name="refresh" content="{$WebDisplayPeriod}" /> 
<title>EC RTPPM Feed</title> 
<link rel="stylesheet" type="text/css" href="stylesheet.css" /> 
<link rel="stylesheet" type="text/css" href="xslstylesheet.css" /> 
</head> 

<body> 
<p>Hello</p> 
     <p>Hello</p> 
</body> 
</html> 
</xsl:template > 
+0

给我500错误,但我无法解决为什么? – bytejunkie 2010-08-09 09:13:24

+0

嫌疑人。像描述的那样工作 – bytejunkie 2010-08-09 09:15:22

短期和明确的答案是,你需要指定attribute轴(或简称@)访问属性,如:

/nr:RTPPMDataMsgV1/nr:RTPPMData/nr:NationalPage/@WebDisplayPeriod 
+0

你会认为我应该能够得到 snapshotTStamp与以下?因为我在同一个模板中创建了一个变量,所以我无法得到它的值。 /nr:RTPPMDataMsgV1/nr:RTPPMData/@ snapshotTStamp – bytejunkie 2010-08-11 07:36:00

+0

@shofty:是的,看起来这是正确的路径。 – 2010-08-11 13:24:31