2010-08-09 63 views
2

以前從來沒有爲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。

回答

1

嘗試它是這樣的:

<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

0

短期和明確的答案是,你需要指定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