我需要帮助格式化 xml 文件,并被要求创建一个 xslt 文件



我需要帮助格式化XML文件,并被要求创建一个XSLT文件以在浏览器中查看,它不必非常花哨。 我已经创建了XML代码,我有一个示例XSLT文件,并认为我可以交换我的作业的值,但无法显示任何内容。 任何帮助将不胜感激。

不确定我做错了什么。

这是我作业的 XSLT 文件。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
    <xsl:for-each select="/SEASON/PLAYER">
      <div style="background-color:teal;color:white;padding:6px">
        <span style="font-weight:bold"><xsl:value-of select="LEAGUE_NAME"/> League- </span>
      </div>
      <div style="background-color:teal;color:white;padding:4px">
        <span style="font-weight:bold"><xsl:value-of select="DIVISION_NAME"/> Division- </span>
      </div>
      <div style="margin-left:22px;margin-bottom:1em;font-size:12pt">
        <p>
          <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
            <span style="font-weight:bold"><xsl:value-of select="Team_NAME"/> Team- </span>
          </div>
          <span style="font-style:italic"> (<xsl:value-of select="GIVEN_NAME/SURNAME"/>PLayer Name)</span>
        </p>
      </div>
    </xsl:for-each>
  </body>
  </html>

这是我的作业 xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Season.xslt" ?>
  <SEASON>
    <YEAR>1998</YEAR>
    <LEAGUE>
      <LEAGUE_NAME>National</LEAGUE_NAME>
      <DIVISION>
        <DIVISION_NAME>East</DIVISION_NAME>
        <TEAM>
          <TEAM_CITY>Florida</TEAM_CITY>
          <TEAM_NAME>Marlins</TEAM_NAME>
          <PLAYER>
            <GIVEN_NAME>Eric</GIVEN_NAME>
            <SURNAME>Ludwick</SURNAME>
            <POSITION>Starting Pitcher</POSITION>
            <GAMES>13</GAMES>
            <GAMES_STARTED>6</GAMES_STARTED>
            <WINS>1</WINS>
            <LOSSES>4</LOSSES>
            <SAVES>0</SAVES>
            <COMPLETE_GAMES>0</COMPLETE_GAMES>
            <SHUT_OUTS>0</SHUT_OUTS>
            <ERA>7.44</ERA>
            <INNINGS>32.2</INNINGS>
            <EARNED_RUNS>31</EARNED_RUNS>
            <HIT_BATTER>27</HIT_BATTER>
            <WILD_PITCHES>0</WILD_PITCHES>
            <BALK>2</BALK>
            <WALKED_BATTER>0</WALKED_BATTER>
            <STRUCK_OUT_BATTER>17</STRUCK_OUT_BATTER>
          </PLAYER>
          <PLAYER>
            <GIVEN_NAME>Brian</GIVEN_NAME>
            <SURNAME>Daubach</SURNAME>
            <POSITION>First Base</POSITION>
            <GAMES>10</GAMES>
            <GAMES_STARTED>3</GAMES_STARTED>
            <AT_BATS>15</AT_BATS>
            <RUNS>0</RUNS>
            <HITS>3</HITS>
            <DOUBLES>1</DOUBLES>
            <TRIPLES>0</TRIPLES>
            <HOME_RUNS>0</HOME_RUNS>
            <RBI>3</RBI>
            <STEALS>0</STEALS>
            <CAUGHT_STEALING>0</CAUGHT_STEALING>
            <SACRIFICE_HITS>0</SACRIFICE_HITS>
            <SACRIFICE_FLIES>0</SACRIFICE_FLIES>
            <ERRORS>0</ERRORS>
            <WALKS>1</WALKS>
            <STRUCK_OUT>5</STRUCK_OUT>
            <HIT_BY_PITCH>1</HIT_BY_PITCH>
          </PLAYER>
        </TEAM>
        <TEAM>
          <TEAM_CITY>Montreal</TEAM_CITY>
          <TEAM_NAME>Expos</TEAM_NAME>
        </TEAM>
        <TEAM>
          <TEAM_CITY>New York</TEAM_CITY>
          <TEAM_NAME>Mets</TEAM_NAME>
        </TEAM>
        <TEAM>
          <TEAM_CITY>Philadelphia</TEAM_CITY>
          <TEAM_NAME>Phillies</TEAM_NAME>
        </TEAM>
      </DIVISION>
      <DIVISION>
        <DIVISION_NAME>Central</DIVISION_NAME>
        <TEAM>
          <TEAM_CITY>Chicago</TEAM_CITY>
          <TEAM_NAME>Cubs</TEAM_NAME>
        </TEAM>
      </DIVISION>
      <DIVISION>
        <DIVISION_NAME>West</DIVISION_NAME>
        <TEAM>
          <TEAM_CITY>Arizona</TEAM_CITY>
          <TEAM_NAME>Diamondbacks</TEAM_NAME>
        </TEAM>
      </DIVISION>
    </LEAGUE>
    <LEAGUE>
      <LEAGUE_NAME>American</LEAGUE_NAME>
      <DIVISION>
        <DIVISION_NAME>East</DIVISION_NAME>
        <TEAM>
          <TEAM_CITY>Baltimore</TEAM_CITY>
          <TEAM_NAME>Orioles</TEAM_NAME>
        </TEAM>
      </DIVISION>
      <DIVISION>
        <DIVISION_NAME>Central</DIVISION_NAME>
        <TEAM>
          <TEAM_CITY>Chicago</TEAM_CITY>
          <TEAM_NAME>White Sox</TEAM_NAME>
        </TEAM>
      </DIVISION>
      <DIVISION>
        <DIVISION_NAME>West</DIVISION_NAME>
        <TEAM>
          <TEAM_CITY>Anaheim</TEAM_CITY>
          <TEAM_NAME>Angels</TEAM_NAME>
        </TEAM>
      </DIVISION>
    </LEAGUE>
  </SEASON>

我试图让它看起来与我的其他作业相似

这是我用于早餐 XML 文件的示例 XSLT 文件。

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
  <xsl:for-each select="breakfast_menu/food">
    <div style="background-color:teal;color:white;padding:4px">
      <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
      <xsl:value-of select="price" />
    </div>
    <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
      <p>
        <xsl:value-of select="description" />
        <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
      </p>
    </div>
  </xsl:for-each>
</body>
</html>

你从这样做开始...

<xsl:for-each select="/SEASON/PLAYER">

但这将寻找一个名为PLAYER的元素,它是SEASON的直接子级。但是PLAYER元素嵌套在LEAGUEDIVISIONTEAM中,所以你的xsl:for-each应该是这样的

<xsl:for-each select="/SEASON/LEAGUE/DIVISION/TEAM/PLAYER">

但是,在此范围内,您随后会这样做。

<xsl:value-of select="LEAGUE_NAME"/>

这是一个问题现在定位在PLAYER元素上,因此它将查找一个名为LEAGUE_NAME的直接子元素,其中没有。您需要向上导航以获取联赛名称

<xsl:value-of select="../../../LEAGUE_NAME"/>

(..获取父节点的位置(

试试这个 XSLT...

<xsl:for-each select="/SEASON/LEAGUE/DIVISION/TEAM/PLAYER">
  <div style="background-color:teal;color:white;padding:6px">
    <span style="font-weight:bold"><xsl:value-of select="../../../LEAGUE_NAME"/> League- </span>
  </div>
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="../../DIVISION_NAME"/> Division- </span>
  </div>
  <div style="margin-left:22px;margin-bottom:1em;font-size:12pt">
    <p>
      <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
        <span style="font-weight:bold"><xsl:value-of select="../TEAM_NAME"/> Team- </span>
      </div>
      <span style="font-style:italic"> (<xsl:value-of select="concat(GIVEN_NAME, ' ', SURNAME)"/>)</span>
    </p>
  </div>
</xsl:for-each>

但是,也许您真的想避免重复联赛,分区和团队?在这种情况下,您可以嵌套xsl:for-each语句

<xsl:for-each select="/SEASON/LEAGUE[.//PLAYER]">
  <div style="background-color:teal;color:white;padding:6px">
    <span style="font-weight:bold"><xsl:value-of select="LEAGUE_NAME"/> League- </span>
  </div>
  <xsl:for-each select="DIVISION[.//PLAYER]">
    <div style="background-color:teal;color:white;padding:4px">
      <span style="font-weight:bold"><xsl:value-of select="DIVISION_NAME"/> Division- </span>
    </div>
    <div style="margin-left:22px;margin-bottom:1em;font-size:12pt">
      <p>
        <xsl:for-each select="TEAM[PLAYER]">
          <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
            <span style="font-weight:bold"><xsl:value-of select="TEAM_NAME"/> Team- </span>
          </div>
          <xsl:for-each select="PLAYER">
            <span style="font-style:italic"> (<xsl:value-of select="concat(GIVEN_NAME, ' ', SURNAME)"/>)</span>
          </xsl:for-each>
        </xsl:for-each>
      </p>
    </div>
  </xsl:for-each>
</xsl:for-each>

请注意条件的使用 [.//PLAYER]以便仅当至少有一个玩家作为后代时才输出元素,例如 LEAGUE

相关内容

  • 没有找到相关文章

最新更新