合并两个 XML,然后转换合并的 xml
我正在研究 XSLT 转换,该转换包括合并两个 XML,然后使用另一个模板转换结果。这个想法是,根据原始 XML 文件的字段,第二个 XML 会发生变化。例如:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<information>
<stmtinfo>
<type>80</type>
<language>2</language>
<clientnum>15907</clientnum>
<clientname>bogus</clientname>
<clientaddress>1401 Barnacle Street, Miami, Fl</clientaddress>
</stmtinfo>
</information>
</data>
根据语言节点的值(我们称之为 LANG),应附加一个名为 language{LANG}.xml 的文件。这些文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<labels>
<l1001>cliente</l1001>
<l1002>moneda de referencia</l1002>
</labels>
变成这样的内容:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<information>
<stmtinfo>
<type>80</type>
<language>2</language>
<clientnum>15907</clientnum>
<clientname>bogus</clientname>
<clientaddress>1401 Barnacle Street, Miami, Fl</clientaddress>
</stmtinfo>
</information>
<labels>
<l1001>cliente</l1001>
<l1002>moneda de referencia</l1002>
</labels>
</document>
其结果应该被转换。我创建了以下模板来进行合并:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:java="http://xml.apache.org/xalan/java/java.lang" xmlns:xfc="http://www.metafocus.no/digiforms/component">
<xsl:template match="data">
<document>
<xsl:call-template name="copy"/>
<xsl:variable name="label_file_name" select="concat('Labels_',information/stmtinfo/language,'.xml')"/>
<xsl:variable name="labels">
<xsl:copy-of select="document($label_file_name)"/>
</xsl:variable>
<xsl:for-each select="xalan:nodeset($labels)/node()">
<xsl:call-template name="copy"/>
</xsl:for-each>
</document>
</xsl:template>
<xsl:template name="copy">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
如果我使用该模板,我将获得以下内容:
<?xml version="1.0" encoding="UTF-8"?><document xmlns:xfc="http://www.metafocus.no/digiforms/component" xmlns:java="http://xml.apache.org/xalan/java/java.lang" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xalan="http://xml.apache.org/xalan"><data>
<information>
<stmtinfo>
<type>80</type>
<language>2</language>
<clientnum>15907</clientnum>
<clientname>bogus</clientname>
<clientaddress>1401 Barnacle Street, Miami, Fl</clientaddress>
</stmtinfo>
</information>
</data><labels>
<l1001>cliente</l1001>
<l1002>moneda de referencia</l1002>
</labels></document>
这似乎是正确的。但是,如果我从其他模板调用,为了处理生成的 XML,
<xsl:template match="/">
<xsl:variable name="link1">
<xsl:apply-templates mode="link1" select="node()"/>
</xsl:variable>
<xsl:value-of select="xalan:nodeset($link1)"/>
</xsl:template>
我将获得以下
80 2 15907 虚假 1401 Barnacle Street, Miami, Fl
因此,合并似乎无法正常工作,因为如果我将结果转换为节点集,节点“标签”就会消失。我做错了什么?
非常感谢。
何塞
I'm working on a XSLT transformation that consists on merging two XML and then transform the result using another template. The idea is that depending on a field of the original XML file, the secong XML changes. For example:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<information>
<stmtinfo>
<type>80</type>
<language>2</language>
<clientnum>15907</clientnum>
<clientname>bogus</clientname>
<clientaddress>1401 Barnacle Street, Miami, Fl</clientaddress>
</stmtinfo>
</information>
</data>
depending on the value of the language node (let's call it LANG), a file callled language{LANG}.xml should appended. The content of those files is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<labels>
<l1001>cliente</l1001>
<l1002>moneda de referencia</l1002>
</labels>
into something like this:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<information>
<stmtinfo>
<type>80</type>
<language>2</language>
<clientnum>15907</clientnum>
<clientname>bogus</clientname>
<clientaddress>1401 Barnacle Street, Miami, Fl</clientaddress>
</stmtinfo>
</information>
<labels>
<l1001>cliente</l1001>
<l1002>moneda de referencia</l1002>
</labels>
</document>
And the result of that should be transformed. I have created the following template to do the merge:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:java="http://xml.apache.org/xalan/java/java.lang" xmlns:xfc="http://www.metafocus.no/digiforms/component">
<xsl:template match="data">
<document>
<xsl:call-template name="copy"/>
<xsl:variable name="label_file_name" select="concat('Labels_',information/stmtinfo/language,'.xml')"/>
<xsl:variable name="labels">
<xsl:copy-of select="document($label_file_name)"/>
</xsl:variable>
<xsl:for-each select="xalan:nodeset($labels)/node()">
<xsl:call-template name="copy"/>
</xsl:for-each>
</document>
</xsl:template>
<xsl:template name="copy">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
If I user that template, I obtain the following:
<?xml version="1.0" encoding="UTF-8"?><document xmlns:xfc="http://www.metafocus.no/digiforms/component" xmlns:java="http://xml.apache.org/xalan/java/java.lang" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xalan="http://xml.apache.org/xalan"><data>
<information>
<stmtinfo>
<type>80</type>
<language>2</language>
<clientnum>15907</clientnum>
<clientname>bogus</clientname>
<clientaddress>1401 Barnacle Street, Miami, Fl</clientaddress>
</stmtinfo>
</information>
</data><labels>
<l1001>cliente</l1001>
<l1002>moneda de referencia</l1002>
</labels></document>
Which seems correct. But if I invocated from other template, to process the resulting XML
<xsl:template match="/">
<xsl:variable name="link1">
<xsl:apply-templates mode="link1" select="node()"/>
</xsl:variable>
<xsl:value-of select="xalan:nodeset($link1)"/>
</xsl:template>
I'm obtaining the following
80
2
15907
bogus
1401 Barnacle Street, Miami, Fl
So it seems that the merge does not work correctly, since if I convert the result to a nodeset, the node "labels" disappears. What I'm doing wrong?
Thank you very much.
Jose
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你为什么首先要进行两遍变换?在我看来,如果您只是将:
放在文档顶部,那么您可以在模板中显式引用
$labels
。第一次转换的结果在结构上与源 XML 没有太大不同,因此通过两次转换实际上除了增加复杂性之外什么也没有得到。也就是说,所写转换中的问题几乎肯定是您正在这样做:
而不是这样:
元素的值是其所有后代文本节点的串联,这就是您在输出中得到的内容。
Why are you doing a two-pass transform in the first place? It seems to me that if you just put:
at the top of your document, then you can reference
$labels
explicitly in your templates. The result of the first pass of your transform isn't different enough in structure from the source XML that you're really gaining anything but complexity from doing it in two passes.That said, the problem in the transform as written is almost certainly that you're doing this:
instead of this:
The value of an element is the concatenation of all of its descendant text nodes, and that's what you're getting in your output.
我通过执行以下操作简化了转换:
但我没有填充标签。
如果我不使用 /l1101,我会得到以下信息:
所以我的印象是导入的 xml 被视为文本,而不是节点集
I simplied the transformation by doing the following:
but I'm not getting the label filled.
If I don't use the /l1101 I get the following:
So I'm under the impression that the xml imported is being treated as a text, not as a nodeset