使用 XSLT 遗漏 XML 中的数据

Leaving out data from XML with XSLT

我是 Whosebug 的新手,也是 XML 和 XSLT 的新手。 你们已经帮助我解决了很多问题,但我需要解决 1 个小问题,我不知道如何解释,所以我可能没有关于这个主题的问题或答案...

这是我的问题:

我正在为一个使用特殊保龄球联赛软件的保龄球联赛开发网站。它以 PDF 或 1 个 XML 大文件输出所有内容。 这是一个干净利落的 XML 但它有这三行文本(联赛名称-缩写-最后更新日期),我不想在应用 XSLT 后显示它们,但我无法找出正确的命令这...

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet  type="text/xsl" href="scores.xsl"?>
<leagueScores>
    <name><![CDATA[Bowling Stones]]></name>
    <abbrev>WDOT</abbrev>
    <date>12-18-2014</date>
    <Rankings>
        <Rank>
            <Place>1</Place>
            <Team><![CDATA[Jaguars]]></Team>
            <VirtualRanking>2</VirtualRanking>
            <Points>36</Points>
            <Pins>9649</Pins>
            <YearAverage>1861</YearAverage>
        </Rank>
...
</leagueScores>

所以我想删除这些:

<name><![CDATA[Bowling Stones Wommelgem Donderdag Trio's]]></name>
<afkorting>WDOT</afkorting>
<date>12-18-2014</date>

我应该怎么做?

这是我正在使用的 XSL 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



<xsl:template match="/leagueScores/Rankings">
<html>
  <body>
  <h1>Ploegenrangschikking</h1>
  <table border="1">
    <tr>
      <th>Plaats</th>
      <th>Team</th>
      <th>Virtueel</th>
      <th>Punten</th>
      <th>Kegels</th>
      <th>Jaar AVG</th>
    </tr>
    <xsl:for-each select="Rank">
    <tr>
      <td><xsl:value-of select="Place"/></td>
      <td><xsl:value-of select="Team"/></td>
      <td><xsl:value-of select="VirtualRanking"/></td>
      <td><xsl:value-of select="Points"/></td>
      <td><xsl:value-of select="Pins"/></td>
      <td><xsl:value-of select="YearAverage"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/TeamScores">
<html>
  <body>
  <h1>Individuele Uitslagen</h1>
  <table border="1">
    <tr>
      <th>Naam</th>
      <th>G1</th>
      <th>G2</th>
      <th>G3</th>
      <th>TOT S</th>
      <th>TOT H</th>
      <th>HDC</th>
      <th>Hi Serie</th>
      <th>Hi Game</th>
    </tr>
    <xsl:for-each select="TeamScore/PlayerScores/PlayerScore">
    <xsl:if test="Game1 &gt; 0">
    <tr>
      <td><xsl:value-of select="Name"/></td>
      <td><xsl:value-of select="Game1"/></td>
      <td><xsl:value-of select="Game2"/></td>
      <td><xsl:value-of select="Game3"/></td>
      <td><xsl:value-of select="Tot"/></td>
      <td><xsl:value-of select="TotHDC"/></td>
      <td><xsl:value-of select="HDC"/></td>
      <td><xsl:value-of select="HighSerie"/></td>
      <td><xsl:value-of select="HighGame"/></td>
    </tr>
    </xsl:if>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/Records">
<html>
  <body>
  <h1>Records</h1>
  <table border="1">
    <tr>
      <th>Categorie</th>
      <th>Naam</th>
      <th>Score</th>
    </tr>
    <xsl:for-each select="Record">
    <tr>
      <td><xsl:value-of select="Category"/></td>
      <td><xsl:value-of select="Name"/></td>
      <td><xsl:value-of select="Score"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/MenIndividualScores">
<html>
  <body>
  <h1>Spelersrangschikking Mannen</h1>
  <table border="1">
    <tr>
      <th>Plaats</th>
      <th>Naam</th>
      <th>Games</th>
      <th>Kegels</th>
      <th>Vorig AVG</th>
      <th>Huidig AVG</th>
      <th>Totaal AVG</th>
      <th>Verschil</th>
      <th>HDP</th>
    </tr>
    <xsl:for-each select="MenIndividualScore">
    <tr>
      <td><xsl:value-of select="Place"/></td>
      <td><xsl:value-of select="Name"/></td>
      <td><xsl:value-of select="Games"/></td>
      <td><xsl:value-of select="Pins"/></td>
      <td><xsl:value-of select="previousAVG"/></td>
      <td><xsl:value-of select="currentAVG"/></td>
      <td><xsl:value-of select="totalAVG"/></td>
      <td><xsl:value-of select="AVGdiff"/></td>
      <td><xsl:value-of select="HDC"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/WomenIndividualScores">
<html>
  <body>
  <h1>Spelersrangschikking Vrouwen</h1>
  <table border="1">
    <tr>
      <th>Plaats</th>
      <th>Naam</th>
      <th>Games</th>
      <th>Kegels</th>
      <th>Vorig AVG</th>
      <th>Huidig AVG</th>
      <th>Totaal AVG</th>
      <th>Verschil</th>
      <th>HDP</th>
    </tr>
    <xsl:for-each select="WomenIndividualScore">
    <tr>
      <td><xsl:value-of select="Place"/></td>
      <td><xsl:value-of select="Name"/></td>
      <td><xsl:value-of select="Games"/></td>
      <td><xsl:value-of select="Pins"/></td>
      <td><xsl:value-of select="previousAVG"/></td>
      <td><xsl:value-of select="currentAVG"/></td>
      <td><xsl:value-of select="totalAVG"/></td>
      <td><xsl:value-of select="AVGdiff"/></td>
      <td><xsl:value-of select="HDC"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/Calendar">
<html>
  <body>
    <xsl:for-each select="Calendar">
    <tr>

    </tr>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/Infos">
<html>
  <body>
    <xsl:for-each select="Info">
    <tr>

    </tr>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/BestPerfomancesOfTheWeek">
<html>
  <body>
  <h1>Beste Weekprestaties</h1>
  <table border="1">
    <tr>
      <th>Categorie</th>
      <th>Naam</th>
      <th>Score</th>
    </tr>
    <xsl:for-each select="Performance">
    <tr>
      <td><xsl:value-of select="Category"/></td>
      <td><xsl:value-of select="Name"/></td>
      <td><xsl:value-of select="Score"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="/leagueScores/DayResults">
<html>
  <body>
  <h1>Matchuitslagen</h1>
  <table border="1">
    <tr>
      <th>Team</th>
      <th>TOT HDC</th>
      <th>Punten</th>
      <th>Team</th>
      <th>TOT HDC</th>
      <th>Punten</th>
    </tr>
    <xsl:for-each select="WomenIndividualScore">
    <tr>
      <td><xsl:value-of select="Team1"/></td>
      <td><xsl:value-of select="TotHDC1"/></td>
      <td><xsl:value-of select="Points1"/></td>
      <td><xsl:value-of select="Team2"/></td>
      <td><xsl:value-of select="TotHDC1"/></td>
      <td><xsl:value-of select="Points2"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>



</xsl:stylesheet>

提前致谢。 公鸡

删除它们的一种方法是添加一个匹配它们的空模板:

<xsl:template match="/leagueScores/name | /leagueScores/abbrev | /leagueScores/date"/>

另一种方法是避免在以下位置开始模板规则:

<xsl:template match="/leagueScores/Rankings">

这会导致 built-in templates 应用于 /leagueScores 节点 - 导致它们的所有文本节点都被复制到输出。

尝试从以下开始:

<xsl:template match="/">

然后做:

<xsl:for-each select="leagueScores/Rankings/Rank">

而不是:

<xsl:for-each select="Rank">

另一种方法是:

<!-- only needed if transforming outside of a browser -->
<!-- <xsl:output method ="html"/>-->

<xsl:template match="/">
     <xsl:apply-templates select="/leagueScores/Rankings"/>
     <xsl:apply-templates select="/leagueScores/TeamScores"/>
     <xsl:apply-templates select="/leagueScores/Records"/>
     <xsl:apply-templates select="/leagueScores/MenIndividualScores"/>
     <xsl:apply-templates select="/leagueScores/WomenIndividualScores"/>
     <xsl:apply-templates select="/leagueScores/Calendar"/>
     <xsl:apply-templates select="/leagueScores/Infos"/>
     <xsl:apply-templates select="/leagueScores/BestPerfomancesOfTheWeek"/>
     <xsl:apply-templates select="/leagueScores/DayResults"/>
</xsl:template>

这样您就不需要更改样式表中的任何其他行。

给定 xml 文件的输出:

<html>
  <body>
    <h1>Ploegenrangschikking</h1>
    <table border="1">
      <tr>
        <th>Plaats</th>
        <th>Team</th>
        <th>Virtueel</th>
        <th>Punten</th>
        <th>Kegels</th>
        <th>Jaar AVG</th>
      </tr>
      <tr>
        <td>1</td>
        <td>Jaguars</td>
        <td>2</td>
        <td>36</td>
        <td>9649</td>
        <td>1861</td>
      </tr>
    </table>
  </body>
</html>