XSLT;仅根据缺少的特定子属性输出父节点

时间:2017-02-02 16:22:05

标签: xml xslt output xslt-1.0 nodes

我是XML和XSL的新手,我似乎一直在盘旋这个问题而且我找不到符合我需求的解决方案。

我有一个以正确的格式和布局显示我的XML数据的XSL。我的问题是,基于CrossSectionSegment部分下缺少的“斜率”字段,我希望XSL忽略某些重复的部分。

我的XML数据的剪辑,其中1个点带有“斜率”字段,1个没有(成千上万个):

<?xml version="1.0" encoding="iso-8859-1"?>
<InRoads productName="Power InRoads V8i (SELECTseries 2)" productVersion="08.11.07.615" outputGridScaleFactor="1.000000" inputGridScaleFactor="1.000000" linearUnits="Metric" angularUnits="Degrees" commandName="Cross Section Report">
    <CrossSectionSet setID="12" setName="assiginack_1" alignmentOID="{F3AC8C7A-D9A2-44C3-B154-611FA05E38F6}" alignmentName="assiginack">
        <CrossSectionStations>
            <CrossSectionStation leftOffset="-9.345000" rightOffset="7.683000" tangentialDirection="0.918685" radialDirection="2.489481" northing="5058046.825198" easting="346212.772114" elevation="0.000000" longitudinalGrade="0.000000">
                <Station internalStation="10300.000000" externalStationName="" externalStation="10300.000000"/>
                <CrossSectionSurfaces>
                    <CrossSectionSurface name="Assi_Design" type="1">
                        <CrossSectionPoints>
                          <CrossSectionPoint type="CrossSectionPoint" northing="5058044.240230" easting="346214.745845" offset="3.252333" elevation="229.294167">
                                <CrossSectionFeatures>
                                    <CrossSectionFeature name="REP" style="EOP" description="Created by roadway design" aheadLongitudinalSlope="0.038977"/>
                                </CrossSectionFeatures>
                                <CrossSectionSegment width="1.687000" height="-0.038567" length="1.687441" slope="-0.022861"/>
                            </CrossSectionPoint>
                            <CrossSectionPoint type="CrossSectionPoint" northing="5058044.239700" easting="346214.746249" offset="3.253000" elevation="229.294136">
                                <CrossSectionFeatures>
                                    <CrossSectionFeature name="REP" style="EOP" description="Created by roadway design" aheadLongitudinalSlope="0.038977"/>
                                </CrossSectionFeatures>
                                <CrossSectionSegment width="0.000667" height="-0.000031" length="0.000667" averageCrossSlopeArea="0.050057"/>
                            </CrossSectionPoint>
                        </CrossSectionPoints>
                    </CrossSectionSurface>
                </CrossSectionSurfaces> 
            </CrossSectionStation>
        </CrossSectionStations>
    </CrossSectionSet>
</InRoads>  

我打算让XML剪辑中的第二个“REP”横截面点被忽略,并保留第一个基于它具有“斜率”字段而第二个没有的事实。或者,相反可能有效,排除具有averageCrossSlopeArea字段的点。 (我还有其他重复的要点,并没有特别称为“REP”)。

这是我交给的XSL并对其进行了一些修改:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:inr="http://mycompany.com/mynamespace">
    <xsl:include href="../format.xsl"/>
    <xsl:param name="xslRootDirectory" select="inr:xslRootDirectory"/>
    <!-- Unique cross section surfaces -->
    <xsl:variable name="uniqueSurfaceName" select="//CrossSectionSurfaces/CrossSectionSurface[not (@name = preceding::CrossSectionSurface/@name)]/@name"/>    
    <!-- Slope Stake Listing -->
    <xsl:template match="/">
        <xsl:variable name="gridOut" select="inr:SetGridOut(number(InRoads/@outputGridScaleFactor))"/>
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="{$xslRootDirectory}/_Themes/engineer/document.css"/>
                <!-- Title displayed in browser Title Bar -->
                <title lang="en">Slope Stake Listing</title>
            </head>
            <body>
                <xsl:choose>
                    <xsl:when test="$xslShowHelp = 'true'">
                        <xsl:call-template name="StyleSheetHelp"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:for-each select="InRoads">
                            <center>
                                <!-- Report Title -->
                                <h6 lang="en">SLOPE STAKE LISTING</h6>
                                <p lang="en">
                                    <xsl:value-of select="inr:longDate()"/>&#xa0; <xsl:value-of select="inr:longTime()"/>
                                </p>
                            </center>
                            <!-- Cross Section Set Data -->
                            <xsl:for-each select="CrossSectionSet">
                                <table class="margin">
                                    <tbody>
                                        <tr>
                                            <td lang="en">Set Name:&#xa0; </td>
                                            <td><xsl:value-of select="@setName"/></td>
                                        </tr>
                                        <tr>
                                            <td lang="en">Project Units:&#xa0; </td>
                                            <td lang="en">
                                                <xsl:if test="//@linearUnits = 'Imperial'">US Survey Feet</xsl:if>
                                                <xsl:if test="//@linearUnits = 'Metric'">Metric</xsl:if>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td lang="en">Contract No.:&#xa0; </td>
                                            <td>&#xa0;</td>
                                        </tr>
                                        <tr>
                                            <td lang="en">Co./Rte./PM:&#xa0; </td>
                                            <td>&#xa0;</td>
                                        </tr>
                                    </tbody>
                                </table>
                                <hr/>
                                <!-- Cross Section Point Data -->
                                <table class="margin" width="90%">
                                    <xsl:for-each select="$uniqueSurfaceName[../@type = 1]">
                                        <xsl:for-each select="//CrossSectionSurface[@name = current()]">
                                            <xsl:for-each select="CrossSectionPoints">
                                                <!-- Alignment Name -->
                                                <tr>
                                                    <th colspan="11" lang="en">
                                                        Alignment Chain:&#xa0; <xsl:value-of select="../../../../../@alignmentName"/>
                                                    </th>
                                                </tr>
                                                <!-- Station -->
                                                <tr>
                                                    <th colspan="11" lang="en">
                                                        Station:&#xa0; <xsl:value-of select="inr:stationFormat(number(../../../Station/@externalStation), $xslStationFormat,$xslStationPrecision, string(../../../Station/@externalStationName))"/>
                                                    </th>
                                                </tr>
                                                <!-- Feature Name -->
                                                <tr>                                                    
                                                    <xsl:for-each select="CrossSectionPoint[CrossSectionFeatures/CrossSectionFeature/@name] | CrossSectionPoint[not (CrossSectionFeatures/CrossSectionFeature/@name) and (@offset &gt; -0.1 and @offset &lt; 0.1)]">                                                                                                                  
                                                        <th>
                                                            <xsl:value-of select="CrossSectionFeatures/CrossSectionFeature/@name"/>
                                                        </th>
                                                    </xsl:for-each>                                                    
                                                </tr>
                                                <!-- Offset -->
                                                <tr>                                                    
                                                    <xsl:for-each select="CrossSectionPoint[CrossSectionFeatures/CrossSectionFeature/@name] | CrossSectionPoint[not (CrossSectionFeatures/CrossSectionFeature/@name) and (@offset &gt; -0.1 and @offset &lt; 0.1)]">
                                                        <td align="center" style="padding-left:5px;padding-right:5px;white-space:nowrap;">
                                                            <xsl:value-of select="inr:distanceFormat(number(@offset), $xslDistancePrecision)"/>
                                                        </td>
                                                    </xsl:for-each>                                                    
                                                </tr>
                                                <!-- Elevation -->
                                                <tr>                                                    
                                                    <xsl:for-each select="CrossSectionPoint[CrossSectionFeatures/CrossSectionFeature/@name] | CrossSectionPoint[@offset &gt; -0.0001 and @offset &lt; 0.0001]">
                                                        <td align="center" style="padding-left:5px;padding-right:5px;white-space:nowrap;">
                                                            <xsl:value-of select="inr:elevationFormat(number(@elevation), $xslElevationPrecision)"/>
                                                        </td>
                                                    </xsl:for-each>                                                    
                                                </tr>
                                                <!-- Slope -->
                                                <tr>                                                    
                                                    <xsl:for-each select="CrossSectionPoint[CrossSectionFeatures/CrossSectionFeature/@name] | CrossSectionPoint[not (CrossSectionFeatures/CrossSectionFeature/@name) and (@offset &gt; -0.1 and @offset &lt; 0.1)]">
                                                        <td align="center" style="padding-left:5px;padding-right:5px;white-space:nowrap;">
                                                            <xsl:value-of select="inr:alternateGradeFormat(number(CrossSectionSegment/@slope), $xslGradeFormat, $xslGradePrecision, $xslIfSlopeExceeds, $xslAlternateSlopeFormat, $xslAlternateSlopePrecision)"/>
                                                        </td>
                                                    </xsl:for-each>                                                    
                                                </tr>                                             
                                                <tr><td>&#xa0;</td></tr>
                                            </xsl:for-each>
                                        </xsl:for-each>
                                    </xsl:for-each>
                                </table>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:otherwise>
                </xsl:choose>
            </body>
        </html>
    </xsl:template>
    <msxsl:script implements-prefix="inr" language="JScript">
        <![CDATA[
            // This function gets the current date.
            function longDate()
            {
                var today = new Date();
                var month = today.getMonth();
                var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
                var dayOfMonth = today.getDate();
                if (dayOfMonth < 10)
                {
                    dayOfMonth = "0" + dayOfMonth;
                }
                var year = today.getFullYear();
                return monthName[today.getMonth()] + " " + dayOfMonth + ", " + year;
            }
            // This function gets the current time.
            function longTime()
            {
                var today = new Date();
                var hours = today.getHours();
                var setting = "";
                if (hours == 12)
                {
                    setting = "PM";
                }
                if (hours == 24)
                {
                    setting = "AM";
                }
                if (hours < 12)
                {
                    setting = "AM";
                }
                if (hours > 12)
                {
                    setting = "PM";
                    hours = hours - 12;
                }
                var minutes = today.getMinutes();
                if (minutes < 10)
                {
                    minutes = "0" + minutes;
                }
                var seconds = today.getSeconds();
                if (seconds < 10)
                {
                    seconds = "0" + seconds;
                    }
                return hours + ":" + minutes + ":" + seconds + " " + setting;
            }
        ]]>
    </msxsl:script>
    <xsl:template name="StyleSheetHelp">
        <div class="section1">
            <h4 lang="en">Notes</h4>
            <p class="normal1" lang="en">
                You must have created cross sections along your alignment and the cross section set must 
                have the surfaces and features upon which you wish to report displayed.
            </p>
            <p class="normal1" lang="en">
                You can create the XML data file from the <em>Evaluation &gt; Cross Section &gt; Cross 
                Section Report</em> command or from the <em>End-Area Volumes</em> leaf of the <em>
                Evaluation &gt; Cross Section &gt; Cross Sections</em> command by toggling on the <em>
                Create XML Report</em> option on the <em>General</em> leaf.
            </p>
            <p class="normal1" lang="en">
                This report works only with surfaces whose <em>Type</em> on the <em>Main</em> tab of the 
                <em>Surface &gt; Surface Properties</em> command is set to <em>Design</em>.&#xa0; It works 
                best if all features (except the catchpoints) included in the XML data file have 
                names.&#xa0; There should also be a point at the center line for best results.&#xa0; 
                Components are not reported.
            </p>
            <p class="small" lang="en">
                <em>&#xa9; 2010 Bentley Systems, Inc</em>
            </p>
        </div>
    </xsl:template> 
</xsl:stylesheet>

我在这里创建了一个名为format.xsl的格式文件的公共链接: https://dl.dropboxusercontent.com/u/48414836/format.xsl

此图像显示正确和不正确的输出:Improper and Proper Outputs

我非常感谢对此的一些帮助,因为我做了大量的研究,并尝试过来自许多不同网站的多种不同解决方案,但没有成功。

提前致谢。

0 个答案:

没有答案