对一个xml文件进行排序,然后让另一个xml文件跟随
好的。我会试着解释我想做什么 我有一个XSL文件,它读取两个xml文件。两个xml文件都包含单词。一个文件包含英语单词,另一个文件包含西班牙语单词,但已翻译 我已经通过xsl转换打印出了这两个xml文件,并将它们放置在彼此的一侧 现在来谈谈我的小问题。我在英文xml文件中使用–sort,这样单词将按字母顺序排序 现在我想把西班牙语的单词打印出来,类似于英语的单词,这样你就有了翻译的感觉 我可以更改xml文件中的位置,但我感觉像是作弊 这是我的英文xml文件。西班牙语的一个很相似,只是里面有西班牙语单词对一个xml文件进行排序,然后让另一个xml文件跟随,xml,xslt,xpath,Xml,Xslt,Xpath,好的。我会试着解释我想做什么 我有一个XSL文件,它读取两个xml文件。两个xml文件都包含单词。一个文件包含英语单词,另一个文件包含西班牙语单词,但已翻译 我已经通过xsl转换打印出了这两个xml文件,并将它们放置在彼此的一侧 现在来谈谈我的小问题。我在英文xml文件中使用–sort,这样单词将按字母顺序排序 现在我想把西班牙语的单词打印出来,类似于英语的单词,这样你就有了翻译的感觉 我可以更改xml文件中的位置,但我感觉像是作弊 这是我的英文xml文件。西班牙语的一个很相似,只是里面有西班牙
<thesaurus>
<dictionary>
<language>Engelska</language>
<word type="1">Stroll</word>
<word type="2">Tender</word>
<word type="3">Agents</word>
<word type="4">Partial</word>
<word type="5">Pogotype</word>
<word type="6">Pretend</word>
<word type="7">Color</word>
<word type="8">Silent</word>
<word type="9">Foundations</word>
<word type="10">Grain</word>
</dictionary>
</thesaurus>
西班牙文
</dictionary>
</thesaurus>
<word type="1">Paseando</word> <!-- Stroll-->
<word type="2">Tierno</word> <!--Tender -->
<word type="3">Agentes</word> <!--Agents -->
<word type="4">Parcial</word> <!--Partial -->
<word type="5">Logo</word> <!--Logotype -->
<word type="6">Pretender</word> <!-- Pretend-->
<word type="7">Color</word> <!--Color -->
<word type="8">Tímido</word> <!-- Silent-->
<word type="9">Dimientos</word> <!--Foundations -->
<word type="10">Grano</word> <!--Grain -->
</dictionary>
</thesaurus>
这就是我打印出来的方式
<xsl:apply-templates select="$doc1//*/*/word">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
<xsl:apply-templates select="$doc2//*/*/word">
</xsl:apply-templates>
谢谢使用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="dict" select="document('dictionary.xml')"/>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:apply-templates select="*/*/word">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="word">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="$dict/*/*/word[@type = current()/@type]"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
输出:
<html>
<body>
<table>
<tr>
<td>Agents</td>
<td>Agentes</td>
</tr>
<tr>
<td>Color</td>
<td>Color</td>
</tr>
<tr>
<td>Foundations</td>
<td>Dimientos</td>
</tr>
<tr>
<td>Grain</td>
<td>Grano</td>
</tr>
<tr>
<td>Partial</td>
<td>Parcial</td>
</tr>
<tr>
<td>Pogotype</td>
<td>Logo</td>
</tr>
<tr>
<td>Pretend</td>
<td>Pretender</td>
</tr>
<tr>
<td>Silent</td>
<td>Tímido</td>
</tr>
<tr>
<td>Stroll</td>
<td>Paseando</td>
</tr>
<tr>
<td>Tender</td>
<td>Tierno</td>
</tr>
</table>
</body>
</html>
输入:
英文单词XML:
<thesaurus>
<dictionary>
<language>Engelska</language>
<word type="1">Stroll</word>
<word type="2">Tender</word>
<word type="3">Agents</word>
<word type="4">Partial</word>
<word type="5">Pogotype</word>
<word type="6">Pretend</word>
<word type="7">Color</word>
<word type="8">Silent</word>
<word type="9">Foundations</word>
<word type="10">Grain</word>
</dictionary>
</thesaurus>
西班牙语单词XML dictionary.XML:
<thesaurus>
<dictionary>
<word type="1">Paseando</word>
<word type="2">Tierno</word>
<word type="3">Agentes</word>
<word type="4">Parcial</word>
<word type="5">Logo</word>
<word type="6">Pretender</word>
<word type="7">Color</word>
<word type="8">Tímido</word>
<word type="9">Dimientos</word>
<word type="10">Grano</word>
</dictionary>
</thesaurus>
使用:
输出:
<html>
<body>
<table>
<tr>
<td>Agents</td>
<td>Agentes</td>
</tr>
<tr>
<td>Color</td>
<td>Color</td>
</tr>
<tr>
<td>Foundations</td>
<td>Dimientos</td>
</tr>
<tr>
<td>Grain</td>
<td>Grano</td>
</tr>
<tr>
<td>Partial</td>
<td>Parcial</td>
</tr>
<tr>
<td>Pogotype</td>
<td>Logo</td>
</tr>
<tr>
<td>Pretend</td>
<td>Pretender</td>
</tr>
<tr>
<td>Silent</td>
<td>Tímido</td>
</tr>
<tr>
<td>Stroll</td>
<td>Paseando</td>
</tr>
<tr>
<td>Tender</td>
<td>Tierno</td>
</tr>
</table>
</body>
</html>
输入:
英文单词XML:
<thesaurus>
<dictionary>
<language>Engelska</language>
<word type="1">Stroll</word>
<word type="2">Tender</word>
<word type="3">Agents</word>
<word type="4">Partial</word>
<word type="5">Pogotype</word>
<word type="6">Pretend</word>
<word type="7">Color</word>
<word type="8">Silent</word>
<word type="9">Foundations</word>
<word type="10">Grain</word>
</dictionary>
</thesaurus>
西班牙语单词XML dictionary.XML:
<thesaurus>
<dictionary>
<word type="1">Paseando</word>
<word type="2">Tierno</word>
<word type="3">Agentes</word>
<word type="4">Parcial</word>
<word type="5">Logo</word>
<word type="6">Pretender</word>
<word type="7">Color</word>
<word type="8">Tímido</word>
<word type="9">Dimientos</word>
<word type="10">Grano</word>
</dictionary>
</thesaurus>
也许Felipe正在寻找一种解决方案,其中所有语言的整个同义词表都是输入XML。我重新列出了基里尔的解决方案,以迎合这种可能性 下面是我使用的输入,包括西班牙语部分的langauge值
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:apply-templates select="*/*[language='Engelska']/word">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="word">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="//*/*[language='Espagnol']/word[@type = current()/@type]"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
输入:
<?xml version="1.0" encoding="UTF-8"?>
<thesaurus>
<dictionary>
<language>Engelska</language>
<word type="1">Stroll</word>
<word type="2">Tender</word>
<word type="3">Agents</word>
<word type="4">Partial</word>
<word type="5">Pogotype</word>
<word type="6">Pretend</word>
<word type="7">Color</word>
<word type="8">Silent</word>
<word type="9">Foundations</word>
<word type="10">Grain</word>
</dictionary>
<dictionary>
<language>Espagnol</language>
<word type="1">Paseando</word>
<word type="2">Tierno</word>
<word type="3">Agentes</word>
<word type="4">Parcial</word>
<word type="5">Logo</word>
<word type="6">Pretender</word>
<word type="7">Color</word>
<word type="8">Tímido</word>
<word type="9">Dimientos</word>
<word type="10">Grano</word>
</dictionary>
</thesaurus>
输出:与Kirill的相同。也许Felipe正在寻找一种解决方案,其中包含所有语言的整个同义词表都是输入XML。我重新列出了基里尔的解决方案,以迎合这种可能性 下面是我使用的输入,包括西班牙语部分的langauge值
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:apply-templates select="*/*[language='Engelska']/word">
<xsl:sort order="ascending"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="word">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="//*/*[language='Espagnol']/word[@type = current()/@type]"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
输入:
<?xml version="1.0" encoding="UTF-8"?>
<thesaurus>
<dictionary>
<language>Engelska</language>
<word type="1">Stroll</word>
<word type="2">Tender</word>
<word type="3">Agents</word>
<word type="4">Partial</word>
<word type="5">Pogotype</word>
<word type="6">Pretend</word>
<word type="7">Color</word>
<word type="8">Silent</word>
<word type="9">Foundations</word>
<word type="10">Grain</word>
</dictionary>
<dictionary>
<language>Espagnol</language>
<word type="1">Paseando</word>
<word type="2">Tierno</word>
<word type="3">Agentes</word>
<word type="4">Parcial</word>
<word type="5">Logo</word>
<word type="6">Pretender</word>
<word type="7">Color</word>
<word type="8">Tímido</word>
<word type="9">Dimientos</word>
<word type="10">Grano</word>
</dictionary>
</thesaurus>
输出:与Kirill的相同。这里有一个使用键的更有效的解决方案。这也是更一般的,因为我们制作了一本分类的英语词典和一本单独的相应排序的西班牙语词典,因此这两本词典可以用于任何目的,可以并排使用,也可以用于翻译数量有限的单词:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kWordByType" match="word" use="@type"/>
<xsl:variable name="vDictSpanish" select=
"document('file:///c:/temp/delete/spanish.xml')"/>
<xsl:variable name="vrtfSortedEnglish">
<thesaurus>
<dictionary>
<xsl:copy-of select="/*/*/language"/>
<xsl:for-each select="/*/*/word">
<xsl:sort/>
<xsl:copy-of select="."/>
</xsl:for-each>
</dictionary>
</thesaurus>
</xsl:variable>
<xsl:variable name="vSortedEnglish"
select="ext:node-set($vrtfSortedEnglish)"/>
<xsl:template match="dictionary">
<thesaurus>
<xsl:copy-of select="$vSortedEnglish/*/dictionary"/>
<dictionary>
<xsl:copy-of select="$vDictSpanish/*/*/language"/>
<xsl:apply-templates select="$vSortedEnglish/*/*/word"/>
</dictionary>
</thesaurus>
</xsl:template>
<xsl:template match="word">
<xsl:variable name="vType" select="@type"/>
<xsl:for-each select="$vDictSpanish">
<xsl:copy-of select="key('kWordByType', $vType)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
产生所需的正确结果:
注意:当前接受的答案在西班牙语词典中执行线性搜索,以查找每个匹配的西班牙语单词。对所有N个已排序的英语单词执行此操作是一个ON^2算法,其二次复杂度不适合于实际大小的词典
这里介绍的解决方案是使用关键字查找来查找匹配的单词。这有一个O1复杂度,查找所有单词是线性复杂度。因此,提出的解决方案是最优的。这里有一个使用键的更有效的解决方案。这也是更一般的,因为我们制作了一本分类的英语词典和一本单独的相应排序的西班牙语词典,因此这两本词典可以用于任何目的,可以并排使用,也可以用于翻译数量有限的单词:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kWordByType" match="word" use="@type"/>
<xsl:variable name="vDictSpanish" select=
"document('file:///c:/temp/delete/spanish.xml')"/>
<xsl:variable name="vrtfSortedEnglish">
<thesaurus>
<dictionary>
<xsl:copy-of select="/*/*/language"/>
<xsl:for-each select="/*/*/word">
<xsl:sort/>
<xsl:copy-of select="."/>
</xsl:for-each>
</dictionary>
</thesaurus>
</xsl:variable>
<xsl:variable name="vSortedEnglish"
select="ext:node-set($vrtfSortedEnglish)"/>
<xsl:template match="dictionary">
<thesaurus>
<xsl:copy-of select="$vSortedEnglish/*/dictionary"/>
<dictionary>
<xsl:copy-of select="$vDictSpanish/*/*/language"/>
<xsl:apply-templates select="$vSortedEnglish/*/*/word"/>
</dictionary>
</thesaurus>
</xsl:template>
<xsl:template match="word">
<xsl:variable name="vType" select="@type"/>
<xsl:for-each select="$vDictSpanish">
<xsl:copy-of select="key('kWordByType', $vType)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
产生所需的正确结果:
注意:当前接受的答案在西班牙语词典中执行线性搜索,以查找每个匹配的西班牙语单词。对所有N个已排序的英语单词执行此操作是一个ON^2算法,其二次复杂度不适合于实际大小的词典
这里介绍的解决方案是使用关键字查找来查找匹配的单词。这有一个O1复杂度,查找所有单词是线性复杂度。因此,提出的解决方案是最优的。Felipe Otarola:这很容易做到,但是您忘了给我们看相应的西班牙语词典。由于我们中很少有人懂西班牙语,请编辑问题并提供西班牙语词典。我们必须知道哪个西班牙语单词是哪个英语单词的翻译-这个关系必须在字典里。请在您提供了西班牙语词典后解释一下。@DimitreNovatchev只需添加西班牙语词典即可。你说这些关系一定在字典里是什么意思?谢谢Elipe Otarola:我指的是可以用来找到与给定单词对应的另一种语言的东西。在您的例子中,这是type属性。你看,一旦你提供了一本西班牙语词典的样本,这种关系就变得很明显,你马上就找到了解决办法。请,学习如何问一个好问题!!!菲利佩·奥塔罗拉:这很容易做到,但是你忘了给我们看相应的西班牙语词典。由于我们中很少有人懂西班牙语,请编辑问题并提供西班牙语词典。我们必须知道哪个西班牙语单词是哪个英语单词的翻译-这个关系必须在字典里。请在您提供了西班牙语词典后解释一下。@DimitreNovatchev只需添加西班牙语词典即可。你说这些关系一定在字典里是什么意思?谢谢Elipe Otarola:我指的是可以用来找到与给定单词对应的另一种语言的东西。在您的例子中,这是type属性。你看,一旦你提供了西班牙语词典的样本,这种关系就变成了obv
你马上就能找到解决办法。请,学习如何问一个好问题!!!此解决方案仅适用于一个xml?还是我遗漏了什么?@FelipeOtarola,您必须将此模板应用于英语单词XML。这个模板在我的示例中也使用了document函数,它用西班牙语单词加载dictionary.xml,这当然会加载dictionary.xml。我将在两个文件上应用模板,并从xml调用这两个文件。解决它:非常感谢..:此解决方案仅适用于一个xml?还是我遗漏了什么?@FelipeOtarola,您必须将此模板应用于英语单词XML。这个模板在我的示例中也使用了document函数,它用西班牙语单词加载dictionary.xml,这当然会加载dictionary.xml。我将在两个文件上应用模板,并从xml调用这两个文件。解决它:非常感谢..: