使用 XML 和 XSLT

发布于 2024-12-07 16:43:13 字数 4314 浏览 1 评论 0原文

我只是想创建一个带有 XSL 样式表的示例 XML 文件。问题是,当我解析这两个文件时,我得到一个奇怪的输出。

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="mySchema.xsl"?>
<Artists>
  <Artist>
    <BandName>The Cure</BandName>
    <Albums>
      <Album>
        <AlbumTitle>Disintegration</AlbumTitle>
        <Tracks>
          <track>
            <Title>Plain Song</Title>
            <Order>1</Order>
            <Lenght>
              <min>5</min>
              <sec>12</sec>
            </Lenght>
          </track>
          <track>
            <Title>Pictures Of You</Title>
            <Order>2</Order>
            <Lenght>
              <min>7</min>
              <sec>24</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
      <Album>
        <AlbumTitle>Wish</AlbumTitle>
        <Tracks>
          <track>
            <Title>A Letter To Elise</Title>
            <Order>4</Order>
            <Lenght>
              <min>5</min>
              <sec>14</sec>
            </Lenght>
          </track>
          <track>
            <Title>From the Edge of the Deep Green Sea</Title>
            <Order>2</Order>
            <Lenght>
              <min>7</min>
              <sec>45</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
    </Albums>
  </Artist>

  <Artist>
    <BandName>The Pogues</BandName>
    <Albums>
      <Album>
        <AlbumTitle>If I Should Fall from Grace with God</AlbumTitle>
        <Tracks>
          <track>
            <Title>Fairytale of New York</Title>
            <Order>1</Order>
            <Lenght>
              <min>2</min>
              <sec>20</sec>
            </Lenght>
          </track>
          <track>
            <Title>Sit Down by the Fire</Title>
            <Order>13</Order>
            <Lenght>
              <min>4</min>
              <sec>10</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
      <Album>
        <AlbumTitle>Peace And Love </AlbumTitle>
        <Tracks>
          <track>
            <Title>Young Ned Of The Hill</Title>
            <Order>3</Order>
            <Lenght>
              <min>2</min>
              <sec>45</sec>
            </Lenght>
          </track>
          <track>
            <Title>Boat Train</Title>
            <Order>11</Order>
            <Lenght>
              <min>2</min>
              <sec>40</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
    </Albums>
  </Artist>
</Artists>

这是 XSLT 文件:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Artist">
    <html>
      <body>
        <xsl:apply-templates select="BandName"/>
        <br></br>
      </body>
    </html>

  </xsl:template>
  <xsl:template match="BandName">
    <b>Found a band!</b>
  </xsl:template>
</xsl:stylesheet>

当我使用 Visual Studio IDE 渲染这两个文件时,我可以看到 XML 文件如何转换为 XHTML。输出如下所示:

<?xml version="1.0" encoding="utf-8"?>
  <html><body><b>Found a band!</b><br /></body></html>

  <html><body><b>Found a band!</b><br /></body></html>

当我在浏览器中查看此内容时,它很好,但我不满意 << html><身体>< /身体>< /html>显示不止一次。我做错了什么?

谢谢 杰森

I'm just trying to create an example XML file with an XSL style sheet. The problem is, when i parse the two files, i get a strange output.

here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="mySchema.xsl"?>
<Artists>
  <Artist>
    <BandName>The Cure</BandName>
    <Albums>
      <Album>
        <AlbumTitle>Disintegration</AlbumTitle>
        <Tracks>
          <track>
            <Title>Plain Song</Title>
            <Order>1</Order>
            <Lenght>
              <min>5</min>
              <sec>12</sec>
            </Lenght>
          </track>
          <track>
            <Title>Pictures Of You</Title>
            <Order>2</Order>
            <Lenght>
              <min>7</min>
              <sec>24</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
      <Album>
        <AlbumTitle>Wish</AlbumTitle>
        <Tracks>
          <track>
            <Title>A Letter To Elise</Title>
            <Order>4</Order>
            <Lenght>
              <min>5</min>
              <sec>14</sec>
            </Lenght>
          </track>
          <track>
            <Title>From the Edge of the Deep Green Sea</Title>
            <Order>2</Order>
            <Lenght>
              <min>7</min>
              <sec>45</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
    </Albums>
  </Artist>

  <Artist>
    <BandName>The Pogues</BandName>
    <Albums>
      <Album>
        <AlbumTitle>If I Should Fall from Grace with God</AlbumTitle>
        <Tracks>
          <track>
            <Title>Fairytale of New York</Title>
            <Order>1</Order>
            <Lenght>
              <min>2</min>
              <sec>20</sec>
            </Lenght>
          </track>
          <track>
            <Title>Sit Down by the Fire</Title>
            <Order>13</Order>
            <Lenght>
              <min>4</min>
              <sec>10</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
      <Album>
        <AlbumTitle>Peace And Love </AlbumTitle>
        <Tracks>
          <track>
            <Title>Young Ned Of The Hill</Title>
            <Order>3</Order>
            <Lenght>
              <min>2</min>
              <sec>45</sec>
            </Lenght>
          </track>
          <track>
            <Title>Boat Train</Title>
            <Order>11</Order>
            <Lenght>
              <min>2</min>
              <sec>40</sec>
            </Lenght>
          </track>
        </Tracks>
      </Album>
    </Albums>
  </Artist>
</Artists>

and here is the XSLT file:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Artist">
    <html>
      <body>
        <xsl:apply-templates select="BandName"/>
        <br></br>
      </body>
    </html>

  </xsl:template>
  <xsl:template match="BandName">
    <b>Found a band!</b>
  </xsl:template>
</xsl:stylesheet>

When i render the two, using visual studio ide, i can see how the XML file transformed to XHTML. the output looks like this:

<?xml version="1.0" encoding="utf-8"?>
  <html><body><b>Found a band!</b><br /></body></html>

  <html><body><b>Found a band!</b><br /></body></html>

When i look at this in the browser, its fine, but i'm not happy how the < html >< body >< /body >< /html > is displayed more that once. What am i doing wrong?

thanks
jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云雾 2024-12-14 16:43:13

您的 match="Artist" 模板被应用两次,因为您的源文档中有两个 Artist。这就是结果树中产生两个 html 元素的原因。

尝试使用样式表的这个稍微修改过的版本:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Artists">
    <html>
      <body>
        <xsl:apply-templates select="Artist"/>
        <br></br>
      </body>
    </html>    
  </xsl:template>

  <xsl:template match="Artist">
    <xsl:apply-templates select="BandName"/>
  </xsl:template>

  <xsl:template match="BandName">
    <b>Found a band!</b>
  </xsl:template>
</xsl:stylesheet>

Your match="Artist" template is getting applied twice because there are two Artist in your source document. This is what's resulting in two html elements in the result tree.

Try using this slightly modified version of your stylesheet:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Artists">
    <html>
      <body>
        <xsl:apply-templates select="Artist"/>
        <br></br>
      </body>
    </html>    
  </xsl:template>

  <xsl:template match="Artist">
    <xsl:apply-templates select="BandName"/>
  </xsl:template>

  <xsl:template match="BandName">
    <b>Found a band!</b>
  </xsl:template>
</xsl:stylesheet>
清晰传感 2024-12-14 16:43:13

我已将您的问题编辑为我认为的问题所在,即正文和 html 标记被包含两次。那是因为您将 Artist 的模板匹配了两次。由于模板包含 body 和 html 标签,因此它们在输出中包含两次。

您需要使用 for-each 来匹配单个模板中的多个实例。下面是 http://www.w3schools.com/ 的摘录,给出了一个示例:

<xsl:for-each select="catalog/cd">
    <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
    </tr>
</xsl:for-each>

查看 w3school xsl更多基础知识部分:
http://www.w3schools.com/xsl/

I've edited your question to what I think the problem is, that the body and html tag are being included twice. That's because you're matching the template for Artist twice. Since the template includes both the body and html tags, they're being included twice in the output.

You need to use a for-each to match multiple instances in a single template. Below is an excerpt from http://www.w3schools.com/ giving an example:

<xsl:for-each select="catalog/cd">
    <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
    </tr>
</xsl:for-each>

Check out the w3school xsl section for more basics:
http://www.w3schools.com/xsl/

薄荷港 2024-12-14 16:43:13

您的第一个模板匹配多个 Artist 元素。对于它匹配的每个 Artist 元素,它将您的 ... 树输出到结果文档。您可以尝试如下操作。

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

  <xsl:template match="/Artists">
    <html>
      <body>
        <xsl:apply-templates select="Artist/BandName"/>
        <br></br>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="BandName">
    <b>Found a band!</b>
  </xsl:template>

</xsl:stylesheet>

Your first template is matching multiple Artist elements. For each Artist element it matches, it is outputting your <html><body>...</> tree to the result document. You can try something like the following.

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

  <xsl:template match="/Artists">
    <html>
      <body>
        <xsl:apply-templates select="Artist/BandName"/>
        <br></br>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="BandName">
    <b>Found a band!</b>
  </xsl:template>

</xsl:stylesheet>
め七分饶幸 2024-12-14 16:43:13

您有多个 Artist 元素,“select”的匹配将同时获取它们。

You have more than one Artist element and the match for "select" is grabbing both of them.

笑脸一如从前 2024-12-14 16:43:13

可能是最短的解决方案之一(没有模板匹配Artist):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
   <body>
    <xsl:apply-templates/>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="Artist/BandName">
   <b>Found a band!</b><br /><br />
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

Probably one of the shortest possible solutions (no template matching Artist):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <html>
   <body>
    <xsl:apply-templates/>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="Artist/BandName">
   <b>Found a band!</b><br /><br />
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文