在XUL JavaScript中使用XSLT处理器的多个XSL文件

时间:2011-09-02 14:01:59

标签: javascript xml xul xslt

这是我原来的XML文件:

<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
    <CONTACT>
        <FirstName>Arun</FirstName>
        <LastName>Arun_niit</LastName>
        <EMail>nuraaa_iceee@yahoo.co.in</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>Arun</FirstName>
        <LastName>Arun_niit</LastName>
        <EMail>nuraaa_iceee@gmail.com</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>KumarVeera</FirstName>
        <LastName>Veera Kumar</LastName>
        <EMail>KUMARg_8111@yahoo.com</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>MarbellaFunkybuddha</FirstName>
        <LastName>Funkybuddha Marbella</LastName>
        <URL>http://www.facebook.com/profile.php?id=111123301493096451</URL>
    </CONTACT>
    <CONTACT>
        <FirstName>Rangarajkarthik</FirstName>
        <LastName>karthik Rangaraj</LastName>
        <EMail>kart200006@gmail.com</EMail>
        <EMail>karthikrangaraju@yahoo.com</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>Rangaraj</FirstName>
        <LastName>karthik </LastName>
        <EMail>kart200006@gmail.com</EMail>
        <EMail>karthikrangaraju@yahoo.com</EMail>
    </CONTACT>
</CONTACTS>

如果我在Firefox XUL中使用XSLT处理器逐个执行,那么我有4个XSl文件可以正常工作。当我逐个执行所有XSL文件时,它不会生成最终输出。

如果我逐个使用XSL文件,这是上述XML文件的实际输出:

<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT>
<CUS-ID>PDE-ID2</CUS-ID>
<FirstName>Arun</FirstName>
<LastName>Arun_niit</LastName>
<gmail>nuraaa_iceee@gmail.com</gmail>
<yahoo>nuraaa_iceee@yahoo.co.in</yahoo>

<URL/>
<Facebook-ID/>
</CONTACT>
<CONTACT>
<CUS-ID>PDE-ID4</CUS-ID>
<FirstName>KumarVeera</FirstName>
<LastName>Veera Kumar</LastName>
<gmail/>
<yahoo>KUMARg_8111@yahoo.com</yahoo>

<URL/>
<Facebook-ID/>
</CONTACT>
<CONTACT>
<CUS-ID>PDE-ID6</CUS-ID>
<FirstName>MarbellaFunkybuddha</FirstName>
<LastName>Funkybuddha Marbella</LastName>
<gmail/>
<yahoo/>

<URL>http://www.facebook.com/profile.php?id=111123301493096451</URL>
<Facebook-ID>1123301493096451</Facebook-ID>
</CONTACT>
<CONTACT>
<CUS-ID>PDE-ID12</CUS-ID>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<gmail>kart200006@gmail.com</gmail>
<yahoo>karthikrangaraju@yahoo.com</yahoo>
<URL/>
<Facebook-ID/>
</CONTACT>
</CONTACTS>

这是在XUL中调用XSl文件的实际JS函数:

function process()
{
    var src = readFile("D:\\createXML2.xsl");
    var parsed = (new DOMParser()).parseFromString(src, "text/xml");
    var stylesheet = parsed.documentElement;

    var processor = new XSLTProcessor();
    processor.importStylesheet(stylesheet );

    objXMLDoc = processor.transformToDocument(objXMLDoc);


    var serializer = new XMLSerializer();
    var prettyString = serializer.serializeToString(objXMLDoc);

    saveFile(prettyString, "D:\\aout30.xml");
     //alert('New Contact updated successfully');


      var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                            .getService(Components.interfaces.nsIPromptService);

       prompts.alert(null, "Final creation", "Final creation is done");
process2();
}

这些是我用来生成最终输出的XSL文件:

文件1:将联系人合并到同一个FN&amp; LN

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

<xsl:key name="groupName" match="//CONTACTS/CONTACT" use="concat(FirstName, LastName)" />

<xsl:template match="CONTACTS">

  <CONTACTS>
  <xsl:for-each select="//CONTACTS/CONTACT[generate-id() = generate-id( key('groupName', concat(FirstName, LastName))   [1] ) ]" >
    <xsl:sort select="CONTACT/EMail"  />

      <xsl:call-template name="group">
        <xsl:with-param name="k1" select="FirstName" />
        <xsl:with-param name="k2" select="LastName" />
      </xsl:call-template>

  </xsl:for-each>
  </CONTACTS>
</xsl:template> 

<xsl:template name="group">
<xsl:param name="k1" /> 
<xsl:param name="k2" /> 

  <CONTACT>
  <xsl:for-each select="//CONTACTS/CONTACT[FirstName = $k1][LastName = $k2][1]">

    <xsl:copy-of select="FirstName" />       
    <xsl:copy-of select="LastName" />       
    <!-- here we have the first Email -->
    <xsl:copy-of select="EMail" />       

  </xsl:for-each>
  <xsl:for-each select="//CONTACTS/CONTACT[FirstName = $k1][LastName = $k2][position() &gt; 1]">
    <!-- here we have the next Email -->

    <xsl:copy-of select="EMail" />       

  </xsl:for-each>

    <xsl:for-each select="//CONTACTS/CONTACT[FirstName = $k1][LastName = $k2][position() &gt; 2]">
    <!-- here we have the next Email -->

    <xsl:copy-of select="EMail" />       

  </xsl:for-each>

      <xsl:for-each select="//CONTACTS/CONTACT[FirstName = $k1][LastName = $k2][position() &gt; 3]">
    <!-- here we have the next Email -->

    <xsl:copy-of select="EMail" />       

  </xsl:for-each>

    <xsl:copy-of select="URL" />       

  </CONTACT>

</xsl:template> 
</xsl:stylesheet>

文件2:合并不同联系人的同一电子邮件地址

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:key name="k_ContactsByEmail"
        match="CONTACTS/CONTACT"
        use="EMail"/>

    <xsl:template match="CONTACTS">
        <xsl:copy>
            <xsl:apply-templates select="CONTACT[generate-id()=
                generate-id(key('k_ContactsByEmail',EMail)[1])]
                |
                CONTACT[not(EMail)]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="CONTACT">
        <xsl:copy>
            <xsl:copy-of select="*"/>
            <xsl:copy-of select="
                key('k_ContactsByEmail', EMail)/*
                    [not(.=current()/*)]
                    [not(self::FirstName or self::LastName)]"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

文件3:文件3根据域类型

分隔电子邮件值
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="CONTACT">
        <xsl:copy>

              <xsl:copy-of select="FirstName|LastName|URL"/>
             <Facebook-ID><!-- To extract the ID value from URL/Email -->
            <xsl:choose>
                <xsl:when test="URL">
                    <xsl:value-of select="substring-after(URL,'?id=')"/>
                </xsl:when>
                <xsl:otherwise>
                   <!-- <xsl:value-of select="substring-before(EMail[1],'@')"/>-->
                </xsl:otherwise>
            </xsl:choose>
        </Facebook-ID>
            <EMAILS>
                <xsl:apply-templates select="EMail"/>
            </EMAILS>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="EMail">
        <EMail> <!-- To extract the Emails based on domain value from EMAIL -->
            <Type><xsl:value-of select="substring-before(
                    substring-after(.,'@'),
                    '.')"/>
            </Type>
            <Value><xsl:value-of select="."/></Value>
        </EMail>
    </xsl:template>

</xsl:stylesheet>

文件4:为每个联系人生成cutomer ID,并将电子邮件域标记添加到第3个xsl生成的输出文件中的电子邮件值类型。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />


<!--Identity template to copy content forward-->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>

<xsl:template match="CONTACT">
<xsl:copy>

<CUS-ID>
    <xsl:apply-templates select="." mode="generate-id"/>
    </CUS-ID>


    <FirstName>
        <xsl:value-of select="FirstName"/>
    </FirstName>

    <LastName>
        <xsl:value-of select="LastName"/>
    </LastName>

    <gmail>
            <xsl:value-of select="EMAILS/EMail[Type='gmail']/Value"/>
    </gmail>

    <yahoo>
            <xsl:value-of select="EMAILS/EMail[Type='yahoo']/Value"/>
    </yahoo>


    <URL>
            <xsl:value-of select="URL"/>
    </URL>

    <Facebook-ID>
             <xsl:value-of select="Facebook-ID"/>
    </Facebook-ID>

      </xsl:copy>
</xsl:template>

<xsl:template match="node()" mode="generate-id">
            <xsl:text>PDE-ID</xsl:text>
            <xsl:number level="any" count="node()" format="10000"/> 
</xsl:template>

</xsl:stylesheet>

有时它会产生正确的输出,但大部分时间它都不起作用。 我真的不明白可能是什么问题。是否有可能将所有这些SL文件合并为一个文件?请帮我。谢谢。

1 个答案:

答案 0 :(得分:1)

要结合上面显示的转换,您需要一个node-set()扩展功能,这可能是您的javascript库无法提供的(是吗?)。

但是,至少对于上面显示的输入样本(略微更改以涵盖其他情况),我认为可以通过单个转换执行所有任务。

请注意,当网址节点位于重复的 CONTACT 中时,您的解决方案(以及此解决方案)也无法处理 Facebook-ID 生成的元素。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="k_Name" match="CONTACTS/CONTACT" 
        use="concat(FirstName, LastName)" />
    <xsl:key name="k_Email" match="CONTACTS/CONTACT" 
        use="EMail" />

    <xsl:template match="CONTACTS">
        <xsl:copy>
            <xsl:apply-templates select="
                CONTACT[generate-id()
                = generate-id(key('k_Email',EMail)[1])
                and generate-id()
                = generate-id(key('k_Name',concat(FirstName,LastName))[1])]
                |
                CONTACT[not(EMail)]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="CONTACT">
        <xsl:copy>

            <CUS-ID>
                <xsl:apply-templates select="." mode="generate-id"/>
            </CUS-ID>

            <xsl:copy-of select="FirstName|LastName"/>

            <Facebook-ID><!-- To extract the ID value from URL/Email -->
                <xsl:choose>
                    <xsl:when test="URL">
                        <xsl:value-of select="substring-after(URL,'?id=')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <!-- <xsl:value-of select="substring-before(EMail[1],'@')"/>-->
                    </xsl:otherwise>
                </xsl:choose>
            </Facebook-ID>

            <xsl:apply-templates select="
                (*
                |
                key('k_Email', EMail)/*
                [not(.=current()/*)]
                |
                key('k_Name',concat(FirstName,LastName))/*
                [not(.=current()/*)])
                [not(self::FirstName or self::LastName)]"/>
        </xsl:copy> 
    </xsl:template>

    <xsl:template match="URL">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="EMail">
        <xsl:element name="{substring-before(
            substring-after(.,'@'),
            '.')}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>


    <xsl:template match="node()" mode="generate-id">
        <xsl:text>PDE-ID</xsl:text>
        <xsl:number level="any" count="node()" format="10000"/> 
    </xsl:template>

</xsl:stylesheet>

应用于以下输入时:

<CONTACTS>
    <CONTACT>
        <FirstName>Arun</FirstName>
        <LastName>Arun_niit</LastName>
        <EMail>nuraaa_iceee@yahoo.co.in</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>Arun</FirstName>
                <LastName>Arun_niit</LastName>
                <URL>aaa</URL>
        <EMail>nuraaa_iceee@gmail.com</EMail>
        <EMail>nuraaa_iceee@hotmail.com</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>KumarVeera</FirstName>
        <LastName>Veera Kumar</LastName>
        <EMail>KUMARg_8111@yahoo.com</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>MarbellaFunkybuddha</FirstName>
        <LastName>Funkybuddha Marbella</LastName>
        <URL>http://www.facebook.com/profile.php?id=111123301493096451</URL>
    </CONTACT>
    <CONTACT>
        <FirstName>Rangarajkarthik</FirstName>
        <LastName>karthik Rangaraj</LastName>
        <EMail>kart200006@gmail.com</EMail>
        <EMail>karthikrangaraju@yahoo.com</EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>Rangaraj</FirstName>
        <LastName>karthik </LastName>
        <EMail>kart200006@gmail.com</EMail>
        <EMail>karthikrangaraju@yahoo.com</EMail>
    </CONTACT>
</CONTACTS>

产生

<CONTACTS>
   <CONTACT>
      <CUS-ID>PDE-ID00002</CUS-ID>
      <FirstName>Arun</FirstName>
      <LastName>Arun_niit</LastName>
      <Facebook-ID/>
      <yahoo>nuraaa_iceee@yahoo.co.in</yahoo>
      <URL>aaa</URL>
      <gmail>nuraaa_iceee@gmail.com</gmail>
      <gmai>nuraaa_iceee@hotmail.com</gmai>
   </CONTACT>
   <CONTACT>
      <CUS-ID>PDE-ID00020</CUS-ID>
      <FirstName>KumarVeera</FirstName>
      <LastName>Veera Kumar</LastName>
      <Facebook-ID/>
      <yahoo>KUMARg_8111@yahoo.com</yahoo>
   </CONTACT>
   <CONTACT>
      <CUS-ID>PDE-ID00027</CUS-ID>
      <FirstName>MarbellaFunkybuddha</FirstName>
      <LastName>Funkybuddha Marbella</LastName>
      <Facebook-ID>111123301493096451</Facebook-ID>
      <URL>http://www.facebook.com/profile.php?id=111123301493096451</URL>
   </CONTACT>
   <CONTACT>
      <CUS-ID>PDE-ID00034</CUS-ID>
      <FirstName>Rangarajkarthik</FirstName>
      <LastName>karthik Rangaraj</LastName>
      <Facebook-ID/>
      <gmail>kart200006@gmail.com</gmail>
      <yahoo>karthikrangaraju@yahoo.com</yahoo>
   </CONTACT>
</CONTACTS>