2012-02-03 103 views
2

好的。将尝试解释我想要做什么..对一个xml文件进行排序,并获得另一个xml文件

我有一个XSL文件,读取两个XML文件。这两个xml文件都包含单词。一个文件中英文单词和其他与西班牙语单词(同样的话,但翻译)

我已经设法通过XSL转换打印出两个XML文件,并将它们在彼此的侧面放置。

现在我的小问题。我使用的是按英文xml文件排序,以便字母按字母顺序排列。

现在我想西班牙的话,让你得到一个翻译的感觉打印出类似英语单词。

我只能改变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> 

感谢

+0

Felipe Otarola:这很容易做到,但是你忘了给我们看相应的西班牙字典。由于我们很少有人知道西班牙语,请编辑问题并提供西班牙语词典。我们必须知道哪个西班牙文单词是哪个英文单词的翻译 - 这个关系必须在字典中。请在提供西班牙字典后解释这一点。 – 2012-02-04 04:33:19

+0

@DimitreNovatchev 只需添加西班牙语词典。 你的意思是说这些关系必须在字典中? 谢谢 – Dymond 2012-02-04 07:23:49

+0

Felipe Otarola:我的意思是可以用来查找给定单词的其他语言对应物的东西。在你的情况下,这是'type'属性。而且你看到 - 只要你提供了西班牙字典的样本,这种关系就变得明显了,你马上就能得到解决方案。请学习如何提出一个好问题! – 2012-02-04 14:55:11

回答

2

用途:

<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> 
+0

此解决方案仅适用于一个xml?或者我错过了什么? – Dymond 2012-02-04 10:38:58

+0

@FelipeOtarola,您必须将此模板应用于英文单词XML。此外,这个模板使用'document'函数(在我的示例中,它加载了带有西班牙文单词的'dictionary.xml'),它加载了字典XML。 – 2012-02-04 11:15:42

+0

@FelipeOtarola,我已经更新了我的答案。 – 2012-02-04 11:17:53

1

也许费利佩正在寻找解决方案,其中具有所有语言的整个叙词表是输入XML。为了迎合这种可能性,我重新调整了基里尔的解决方案。

我使用的输入(包括西班牙语部分的语言值)如下。

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

输出:同基里尔的。

1

这是一个更高效的解决方案,使用密钥。它也更一般,因为我们生成一个已排序的英文字典和一个单独的相应排序的西班牙字典,以便这两个字典可用于任何目的 - 并排或翻译有限数量的字词:

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

当这个变换所提供的字典eEnglish 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文档在驻留在文件:c:/temp/delete/spanish.xml

<thesaurus> 
    <dictionary> 
     <language>Spanish</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> 

想要的,正确的结果产生

<thesaurus> 
    <dictionary> 
     <language>Engelska</language> 
     <word type="3">Agents</word> 
     <word type="7">Color</word> 
     <word type="9">Foundations</word> 
     <word type="10">Grain</word> 
     <word type="4">Partial</word> 
     <word type="5">Pogotype</word> 
     <word type="6">Pretend</word> 
     <word type="8">Silent</word> 
     <word type="1">Stroll</word> 
     <word type="2">Tender</word> 
    </dictionary> 
    <dictionary> 
     <language>Spanish</language> 
     <word type="3">Agentes</word> 
     <word type="7">Color</word> 
     <word type="9">Dimientos</word> 
     <word type="10">Grano</word> 
     <word type="4">Parcial</word> 
     <word type="5">Logo</word> 
     <word type="6">Pretender</word> 
     <word type="8">Tímido</word> 
     <word type="1">Paseando</word> 
     <word type="2">Tierno</word> 
    </dictionary> 
</thesaurus> 

请注意:目前公认的答案进行的西班牙语字典查找所有匹配的西班牙语单词线性搜索。对所有N个排序的英文单词做一个O(N^2)算法(具有二次方复杂度)不适用于实际大小的字典。

此处介绍的解决方案是使用密钥查找来查找匹配的单词。这具有O(1)的复杂性,查找所有单词是O(N) - 线性复杂度。因此,所提出的解决方案是最佳的。