为同一个XSL文件生成两个不同的输出? [英] Generating two different outputs for the same XSL file?

查看:108
本文介绍了为同一个XSL文件生成两个不同的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的XML文件,如果我运行XSLt文件,我将在Eclipse XSL Transformation中以相同的顺序获得相同的输出。
即使向我的xml文件添加新记录并运行XSL文件,< xsl:value-of select =generate-id(。)/> 将为新记录创建唯一ID。

This is my XML file and If i run the XSLt file i'll get the same output in the same order exactly in Eclipse XSL Transformation. Even if add a new record to my xml file and run the XSL file, the <xsl:value-of select="generate-id(.)"/> will create unique id for the new record.

<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>

<CONTACT>
<Customer-ID>N65539</Customer-ID>
<FirstName>Ben</FirstName>
<LastName>Foden</LastName>
<email></email>
<address></address>
<state>AZ</state>
<country>US</country>
</CONTACT>

<CONTACT>
<Customer-ID>N65539</Customer-ID>
<FirstName>Nimal</FirstName>
<LastName>Anup</LastName>
<email>nimal.anup@gmail.com</email>
<address></address>
<state>TN</state>
<country>IN</country>
</CONTACT>


<CONTACTS>

这是我更新的XSLT文件:

This is my updated XSLT file:

  <?xml version="1.0"?>
<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>
    <Customer-ID>
    <xsl:apply-templates select="node()" mode="generate-id"/>
    </Customer-ID>

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

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

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

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

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

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

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

    <xsl:template match="node()" mode="generate-id">
    <xsl:text>N</xsl:text>
    <xsl:number level="single" count="node()" format="100"/>        
    </xsl:template>

</xsl:stylesheet>

然后我在XUL中使用相同的XSLT文件用于XSLT处理器功能,我得到了一个不同的ID和输出的类型。
它继续为旧记录和新记录生成新ID如果我在XML文件中添加新记录。

Then I used the same XSLT file for XSLT processor function in XUL, which I'm getting a different type of ID and output. It's keep generating a new ID for old record and for new record If i add a new record in XML file.

如何仅生成新记录为了新记录?如何将输入文件的XML模板与我的XML输出文件相同。

How do i generate a new id only for the new record? and how can i have the same XML template of my input file to my XML output file.

这是我得到的输出:

<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>

<CONTACT><Customer-ID>id0x03e4a620</Customer-ID><FirstName>Ben</FirstName><LastName>Foden</LastName><email></email><address></address><state>AZ</state><country>US</country></CONTACT>

<CONTACT><Customer-ID>id0x03e4ad80</Customer-ID><FirstName>Nimal</FirstName><LastName>Anup</LastName><email>nimal.anup@gmail.com</email><address></address><state>TN</state><country>IN</country></CONTACT>

<CONTACTS>

这是我调用XSLT文件的Javascript:脚本在保存XML文件后调用此函数。新记录将按照输入xml文件中提到的相同更新。

This is my Javascript to call the XSLT file: The script call this function after saving the XML file. The new record will update in the same as mentioned in the input xml file.

function process()
{

var src = readFile("c:\\idgenerator.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, "C:\\mercredi.xml");

} 

非常感谢。

推荐答案

我不知道Eclipse使用的是什么XSLT处理器。至少在某些扩展程序中,您可以配置处理器,例如到Xalan或Saxon。
Mozilla / XUL至少在默认情况下使用Transformiix。我在某处读到可以使用Saxon。
不同的XSLT处理器之间肯定存在差异。

I don't know what XSLT processor Eclipse uses. At least with some extensions, you can configure the processor, e.g. to Xalan or Saxon. Mozilla/XUL uses Transformiix, at least by default. I read somewhere that it could be made to use Saxon. There are certainly differences between different XSLT processors.

generate-id()没有说明生成的id应该是什么样的;只是它们必须由ASCII字母数字字符组成,并且必须以字母字符开头。

The specification for generate-id() does not say what the generated ids should look like; only that they "must consist of ASCII alphanumeric characters and must start with an alphabetic character".

如果您希望XSLT-under-XUL生成相同类型的ID,在Eclipse下,你有几个选择。

If you want XSLT-under-XUL to produce the same kinds of IDs as under Eclipse, you have a couple of options.

1)你可以尝试让XUL使用Eclipse使用的XSLT处理器。我不知道这是否可行。

1) You could try and get XUL to use the XSLT processor that Eclipse uses. I don't know if this is possible.

2)实现自己的自定义模板以生成ID。

2) Implement your own custom template for generating IDs.

使用后者,如果你有一些基于它们的稳定数据,那么模仿你在Eclipse中获得的ID的风格并使它们在多个运行和不同的XSLT处理器中保持稳定并不困难。

With the latter, it would not be hard to imitate the style of the IDs you get in Eclipse, and to make them stable across multiple runs and different XSLT processors, if you have some stable data to base them on.

例如,如果您的记录顺序始终稳定(旧记录永远不会被删除,交换或替换),那么您可以使用这样的模板来生成ID:

For example, if the order of your records is always stable (old records will never be deleted, or swapped, or replaced), then you could use a template like this to generate IDs:

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

如果订单不稳定,但名字+姓氏既稳定又独特,可以使用

If the order is not stable, but the first name + last name is both stable and unique, you could use

<xsl:template match="node()" mode="generate-id">
   <xsl:value-of select="concat(FirstName, LastName)" />
</xsl:template>

这篇关于为同一个XSL文件生成两个不同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆