【发布时间】:2019-10-26 05:20:19
【问题描述】:
我正在尝试获取两个单独的 XML 文件,并使用 XSL 将它们变成一个 HTML 文件。我从第一个 XML 中得到了正确的东西,但是当我从第二个 XML 中尝试相同的内容时,我要么在一行中得到所有单词,要么只得到第一个单词几次。
XML 1
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="xsltcss.css"?>
<?xml-stylesheet type="text/xsl" href="xslt.xsl" ?>
<lexicon>
<head>
<title>Danish</title>
<author>Mattias Liljegren</author>
</head>
<language value="danish">
<word value="dog">hund</word>
<word value="coffee">kaffe</word>
<word value="tree">træ</word>
<word value="chair">stol</word>
<word value="flashlight">lommelygte</word>
<word value="cat">kat</word>
<word value="fish">fisk</word>
<word value="car">bil</word>
<word value="phone">telefon</word>
<word value="forest">skov</word>
</language>
</lexicon>
XML 2
<?xml version="1.0" encoding="UTF-8"?>
<lexicon>
<head>
<title>Croatian</title>
<author>Mattias Liljegren</author>
</head>
<language value="croatian">
<word value="dog">pas</word>
<word value="coffee">kava</word>
<word value="tree">drvo</word>
<word value="chair">stolica</word>
<word value="flashlight">baterija</word>
<word value="cat">mačka</word>
<word value="fish">riba</word>
<word value="car">automobil</word>
<word value="phone">telefon</word>
<word value="forest">šuma</word>
</language>
</lexicon>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="xsltcss.css" />
</head>
<body>
<xsl:for-each select="lexicon/head/title">
<p>
<xsl:value-of select="." />
</p>
<p>
<xsl:value-of select="document('kroatiska.xml')/lexicon/head/title/." />
</p>
</xsl:for-each>
<xsl:for-each select="lexicon/language/word">
<p>
<xsl:value-of select="." />
</p>
</xsl:for-each>
<xsl:for-each select="lexicon/language/word">
<p>
<xsl:value-of select="document('kroatiska.xml')/lexicon/language/word" />
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
两个单独的 xml 文件,我想要每个文件的第一个单词,然后是第二个单词,依此类推。 Atm 我从原始 xml 中得到正确的,但只有第二个 xml 文件中的第一个单词“pas”。
预期结果:
Danish
hund
kaffe
trä
stol
lommelygte
kat
fisk
bil
telefon
skov
Croatian
pas
kava
drvo
stolica
baterija
macka
riba
automobil
telefon
suma
【问题讨论】:
-
转换的预期结果是什么?
-
另外,您真的在使用 XSLT 2.0 处理器吗?您的样式表显示
version="2.0"但此<?xml-stylesheet type="text/xsl" href="xslt.xsl" ?>表明您正在浏览器中进行转换,即 XSLT 1.0。 -
Atm 我将顶部单词作为单独的行,底部单词作为相同数量的行但只有第一个单词 pas,或者当我多次将所有单词放在一行中尝试编辑代码。我希望它从每个 xml 中获取第一个单词,然后从每个 xml 中获取第二个单词,依此类推。我不知道我正在使用哪个处理器,为学校做这个,所以我有点迷路:)
-
恐怕这会令人困惑。请编辑您的问题并添加您期望从给定示例中获得的确切结果。 -- 如果您不知道您使用的是哪个处理器,请查找:stackoverflow.com/questions/25244370/…
-
我现在已经更新了底部,所以我希望它不会那么混乱。我正在使用版本 1!将它从 2.0 编辑到 1,谢谢!