使用xslt如何在xml文件中解决此特定元素?

时间:2010-08-09 08:23:17

标签: 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。

2 个答案:

答案 0 :(得分: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 >

答案 1 :(得分:0)

简短而明确的答案是您需要指定attribute轴(或简称@)来访问属性,例如:

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