简体   繁体   English

我的XSLT有什么问题?

[英]What is wrong my XSLT?

I want to XML to other format XML so I used XSLT. 我想将XML转换为其他格式的XML,所以我使用了XSLT。 But tt became a bad result. 但是tt变成了一个糟糕的结果。

XML: XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>

XSLT : XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" indent="yes" />
<xsl:template match="/">
    <root>
        <items>
            <xsl:for-each select="catalog/cd">
                <item>
                    <xsl:value-of select="artist"/>
                </item>
            </xsl:for-each>
        </items>
    </root>
</xsl:template>
</xsl:stylesheet>

Result I want (in browser): 我想要的结果(在浏览器中):

<?xml version="1.0" encoding="utf-8"?>
<root>
    <items>
        <item>Empire Burlesque</item>
        <item>Hide your heart</item>
        <item>Greatest Hits</item>
    </items>
</root>

Real result (in browser): 实际结果(在浏览器中):

Empire Burlesque Hide your heart Greatest Hits

What is wrong my XSLT? 我的XSLT有什么问题?

I would wager that you are using Firefox, and its trying to render it as HTML which means its removing tags it doesn't understand. 我敢打赌,您正在使用Firefox,并且它试图将其呈现为HTML,这意味着删除了它不了解的标签。 Try right clicking thpage and viewing the source and seeing if the source of the page is correct. 尝试右键单击页面并查看源,然后查看页面源是否正确。

You can change you xsl to this: 您可以将xsl更改为此:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" indent="yes" />
<xsl:template match="/">
    <root>
        <xsl:for-each select="catalog/cd">
            <items>
                <item>
                    <xsl:value-of select="title"/>
                    <xsl:text xml:space="preserve">&#10;</xsl:text>
                </item>
            </items>
        </xsl:for-each>
    </root>
</xsl:template>
</xsl:stylesheet>

And that will generate in the browser this: 这将在浏览器中生成:

Empire Burlesque
Hide your heart
Greatest Hits

And if you click on it, in firefox, and you select web developer > view source > view generated source you will get this: 如果单击它,在firefox中,然后选择web developer > view source > view generated source您将得到以下信息:

<root><items><item>Empire Burlesque
</item></items><items><item>Hide your heart
</item></items><items><item>Greatest Hits
</item></items></root>

which is what you said you want. 这就是你想要的。

Remember that in the browser you will see the text, all the tags that are not html, are discarded. 请记住,在浏览器中您将看到文本,所有不是html的标记都将被丢弃。 If you check the source code, you will see the xml file since that is what you loaded. 如果检查源代码,您将看到xml文件,因为这是您加载的文件。 If you check the generated source is what the transformation is telling the browser to show. 如果检查生成的源,则转换将告诉浏览器显示什么。

By default, the rendering engine of the browser, works with html, that's why it ignores any other tag. 默认情况下,浏览器的呈现引擎与html一起使用,这就是为什么它忽略任何其他标记的原因。

Bye 再见

It seems like you are interested in how it renders in the browser. 您似乎对它如何在浏览器中呈现感兴趣。 In that case, maybe this is what you want... 在这种情况下,也许这就是您想要的...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes" />
<xsl:template match="/">
  <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html&gt;</xsl:text>
  <html>
    <head>
      <meta charset="utf-8" />
      <title>List of CDs</title>
    </head>
    <body>
      <ul>
        <xsl:for-each select="catalog/cd">
          <li><xsl:value-of select="artist"/></li>
      </xsl:for-each>
      </ul>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM