使用XSLT从XML中删除数据

时间:2015-02-16 18:44:30

标签: xml xslt

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

这是我的问题:

我正在网站上为一个使用特殊保龄球联盟软件的保龄球联盟工作。它以PDF或一个大的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>

提前致谢。 鸡

2 个答案:

答案 0 :(得分:0)

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

<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">

答案 1 :(得分:-1)

另一种方法是:

<!-- 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>