繁体   English   中英

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

[英]using xslt how do i address this particular element in an xml file?

以前从来没有为XML编码过,完全被各种入门知识所迷惑,这些入门知识告诉我要做不同的事情。 这可能意味着您可以采用不同的方式来执行此操作,但是它们都显示单节点情况,而事实并非如此。

我尝试了各种方法来挑选WebDisplayPeriod,但是我坚信我实际上并没有针对该权利的属性。 希望有人能帮助我朝正确的方向发展。 同样,由于w3schools是垃圾,因此任何有关XSLT更高级使用的可读入门知识都将受到赞赏;)。

我有以下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 >

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM