使用 XML 和 XSLT
我只是想创建一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的
match="Artist"
模板被应用两次,因为您的源文档中有两个Artist
。这就是结果树中产生两个html
元素的原因。尝试使用样式表的这个稍微修改过的版本:
Your
match="Artist"
template is getting applied twice because there are twoArtist
in your source document. This is what's resulting in twohtml
elements in the result tree.Try using this slightly modified version of your stylesheet:
我已将您的问题编辑为我认为的问题所在,即正文和 html 标记被包含两次。那是因为您将 Artist 的模板匹配了两次。由于模板包含 body 和 html 标签,因此它们在输出中包含两次。
您需要使用 for-each 来匹配单个模板中的多个实例。下面是 http://www.w3schools.com/ 的摘录,给出了一个示例:
查看 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:
Check out the w3school xsl section for more basics:
http://www.w3schools.com/xsl/
您的第一个模板匹配多个 Artist 元素。对于它匹配的每个 Artist 元素,它将您的
...
树输出到结果文档。您可以尝试如下操作。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.您有多个 Artist 元素,“select”的匹配将同时获取它们。
You have more than one Artist element and the match for "select" is grabbing both of them.
可能是最短的解决方案之一(没有模板匹配
Artist
):Probably one of the shortest possible solutions (no template matching
Artist
):