0

XMLを他の形式のXMLにしたいので、XSLTを使用しました。しかし、ttは悪い結果になりました。

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

<?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>

私が欲しい結果(ブラウザで):

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

実際の結果(ブラウザで):

Empire Burlesque Hide your heart Greatest Hits

XSLTの何が問題になっていますか?

4

3 に答える 3

2

私はあなたがFirefoxを使用していて、それをHTMLとしてレンダリングしようとしていることを賭けます。これは、理解できないタグを削除することを意味します。thpageを右クリックしてソースを表示し、ページのソースが正しいかどうかを確認してみてください。

于 2012-10-18T01:28:59.677 に答える
2

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>

そしてそれはブラウザでこれを生成します:

Empire Burlesque
Hide your heart
Greatest Hits

そして、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>

それはあなたが望むとあなたが言ったことです。

ブラウザにテキストが表示され、html以外のすべてのタグが破棄されることに注意してください。ソースコードを確認すると、ロードしたものであるため、xmlファイルが表示されます。生成されたソースを確認すると、変換によってブラウザに表示されるように指示されます。

デフォルトでは、ブラウザのレンダリングエンジンはhtmlで動作するため、他のタグは無視されます。

さよなら

于 2012-10-18T01:57:23.947 に答える
1

ブラウザでどのようにレンダリングされるかに興味があるようです。その場合、多分これはあなたが望むものです...

<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>
于 2012-10-18T02:02:37.830 に答える