使用xslt定位属于素食主义者的属性

时间:2015-09-16 02:19:50

标签: xml xslt

嘿伙计们,我正在使用xml和xslt开展我的第一个项目。现在我想要完成的目标是素食菜单的属性,并在餐馆名称下显示在页面底部。我需要在一行上显示MenuItem,Price和Calories的名称。任何帮助将不胜感激。

好吧,我已经把它全部放在页面底部我应该

小小屋旅馆

菜单项目价格卡路里水果沙拉5.50< 300

Ma和Pa's

菜单项目价格卡路里厨师沙拉5.00 200

这是XML

<?xml version="1.0" encoding="utf-8"?>
<!--Kenny Freeman-->
<Restaurants>
<Restaurant Name="Little Cabin Inn" QualityRating="3" PriceRating="2">
<Location>
  <Address>10 Main Street</Address>
  <City>Chicago</City>
  <State>Illinois</State>
</Location>

<PhoneNumber>555-555-5555</PhoneNumber>

<Menu>
  <MenuItem Price="4.95" Calories="450"  IsVegetarian="false">Hamburger</MenuItem>
  <MenuItem Price="5.50" Calories="&lt;300" IsVegetarian="true">Fruit Salad</MenuItem>
</Menu>
</Restaurant>

<Restaurant Name="Ma and Pa" QualityRating="4" PriceRating="3">
<Location>
  <Address>1234 2nd Street</Address>
  <City>Chicago</City>
  <State>Illinois</State>
</Location>

<PhoneNumber>444-444-4444</PhoneNumber>
<Menu>
<MenuItem Price="4.00" Calories="475" IsVegetarian="false">CheeseBurger</MenuItem>
<MenuItem Price="5.00" Calories="200" IsVegetarian="true">Chef  Salad</MenuItem>
</Menu>
</Restaurant>

<Restaurant Name="Meaty Petes Cafe" QualityRating="4" PriceRating="1">
<Location>
  <Address>1232 Cedar Drive</Address>
  <City>Los Angelas</City>
  <State>California</State>
</Location>

<PhoneNumber>333-333-3333</PhoneNumber>
<Menu>
  <MenuItem Price="6.00" Calories="500" IsVegetarian="false">Stack of Ribs</MenuItem>
</Menu>
</Restaurant>
</Restaurants>

和XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" />

<xsl:template match="/">
<html>
  <head>
    <title>Restaurants</title>
  </head>
  <body>
    <h1>Recommended Restaurant</h1>
    <p>
      In Chicago, I recommend <xsl:value-of select="Restaurants/Restaurant/@Name" />
    </p>
    <h1>Restaurants by location</h1>
    <table border="1">
      <tr>
        <th>Restaurant Name</th>
        <th>City</th>
        <th>State</th>
        <th>Quality</th>
        <th>Price</th>
      </tr>
      <xsl:for-each select="Restaurants/Restaurant">
        <xsl:sort select="Location/State"/>
        <tr>
          <td>
            <xsl:value-of select="@Name"/>
          </td>
          <td>
            <xsl:value-of select="Location/City"/>
          </td>
          <td>
            <xsl:value-of select="Location/State"/>
          </td>
          <td>
            <xsl:for-each select="Restaurants/Restaurant"/>
          <xsl:value-of select="@QualityRating"/>
          </td>
          <td>
        <xsl:for-each select="Restaurants/Restaurant"/>
            <xsl:value-of select="@PriceRating"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  <!--Restaurants that serve vegetarian-->
    <h1>Restaurants with Vegetarian Choices</h1>
  <xsl:for-each select="Restaurants/Restaurant">
  <xsl:if test="Menu/MenuItem/@IsVegetarian='true'">
    <h3>
  <xsl:value-of select="@Name"/>
      <xsl:for-each select="Menu/MenuItem">
        <br>
          <xsl:value-of select="Menu/MenuItem"/>

            <xsl:value-of select="@Price"/>


            <xsl:value-of select="@Calories"/>

        </br>

      </xsl:for-each>

  </h3>

  </xsl:if>
  </xsl:for-each>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:1)

看起来你做错了两件事。

首先,以下代码段不会包含菜单项元素的文本:

<xsl:value-of select="Menu/MenuItem"/>

您已经选择了元素,因此您可以使用以下内容:

<xsl:value-of select="text()"/>

获取内部文本 - 请参阅xslt: value of text associated with element了解更多

其次,虽然您选择的是带素食菜单的餐馆,但听起来您只想展示素食菜单。因此,您需要将循环更改为仅显示这些项目 - 额外的xsl:if语句可以执行此任务,尽管可以更干净地执行此操作。

答案 1 :(得分:1)

这个XSLT 1.0样式表......

<Restaurants>
    <Restaurant Name="Little Cabin Inn" QualityRating="3" PriceRating="2">
        <Location>
            <Address>10 Main Street</Address>
            <City>Chicago</City>
            <State>Illinois</State>
        </Location>
        <PhoneNumber>555-555-5555</PhoneNumber>
        <Menu>
            <MenuItem Price="4.95" Calories="450" IsVegetarian="false">Hamburger</MenuItem>
            <MenuItem Price="5.50" Calories="&lt;300" IsVegetarian="true">Fruit Salad</MenuItem>
        </Menu>
    </Restaurant>
    <Restaurant Name="Ma and Pa" QualityRating="4" PriceRating="3">
        <Location>
            <Address>1234 2nd Street</Address>
            <City>Chicago</City>
            <State>Illinois</State>
        </Location>
        <PhoneNumber>444-444-4444</PhoneNumber>
        <Menu>
            <MenuItem Price="4.00" Calories="475" IsVegetarian="false">CheeseBurger</MenuItem>
            <MenuItem Price="5.00" Calories="200" IsVegetarian="true">Chef  Salad</MenuItem>
        </Menu>
    </Restaurant>
    <Restaurant Name="Meaty Petes Cafe" QualityRating="4" PriceRating="1">
        <Location>
            <Address>1232 Cedar Drive</Address>
            <City>Los Angelas</City>
            <State>California</State>
        </Location>
        <PhoneNumber>333-333-3333</PhoneNumber>
        <Menu>
            <MenuItem Price="6.00" Calories="500" IsVegetarian="false">Stack of Ribs</MenuItem>
        </Menu>
    </Restaurant>
</Restaurants>

...将转换此输入文档......

<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Restaurants</title>
  </head>
  <body>
    <h1>Recommended Restaurant</h1>
    <p>
      In Chicago, I recommend Little Cabin Inn</p>
    <h1>Restaurants by location</h1>
    <table border="1">
      <tr>
        <th>Restaurant Name</th>
        <th>City</th>
        <th>State</th>
        <th>Quality</th>
        <th>Price</th>
      </tr>
      <tr>
        <td>Meaty Petes Cafe</td>
        <td>Los Angelas</td>
        <td>California</td>
        <td>4</td>
        <td>1</td>
      </tr>
      <tr>
        <td>Little Cabin Inn</td>
        <td>Chicago</td>
        <td>Illinois</td>
        <td>3</td>
        <td>2</td>
      </tr>
      <tr>
        <td>Ma and Pa</td>
        <td>Chicago</td>
        <td>Illinois</td>
        <td>4</td>
        <td>3</td>
      </tr>
    </table>
    <h1>Restaurants with Vegetarian Choices</h1>
    <table border="1">
      <tr>
        <th>Restaurant Name</th>
        <th>Menu item</th>
        <th>Price</th>
        <th>Calories</th>
      </tr>
      <tr>
        <td>Ma and Pa</td>
        <td>Chef  Salad</td>
        <td>5.00</td>
        <td>200</td>
      </tr>
      <tr>
        <td>Little Cabin Inn</td>
        <td>Fruit Salad</td>
        <td>5.50</td>
        <td>&lt;300</td>
      </tr>
    </table>
  </body>
</html>

...进入此输出文档...

SELECT wpp.ID,wpp.post_title,wpp.guid,
(SELECT guid FROM wp_posts  WHERE post_parent = (SELECT ID FROM wp_posts WHERE post_title =CONCAT("Writer Thumbnail Image ",wpp.ID) )) AS imgID 
FROM `wp_posts` AS wpp  WHERE wpp.post_status = "publish" AND wpp.post_type = "post"  ORDER BY wpp.ID DESC LIMIT 4

假设

  1. 我不得不猜测所需的输出,因为OP没有列出它。我把所有素食餐馆放在一张桌子上。但是可能需要多个表格?
  2. 我假设推荐的芝加哥餐厅是文件顺序中输入文件中列出的第一个芝加哥餐厅。