如何使用xslt合并两个xml文件以生成文本输出 [英] How to merge two xml files using xslt generating text output
问题描述
我已经在下面看到了我的问题的变体 how-to-merge-two-xml-files-with-xslt 或者 how-can-i-merge-these-two- xml-files-using-xslt ,但是这些示例不处理文本输出,也不处理对'default.xml'的静态引用.
I already saw variants of my problem under how-to-merge-two-xml-files-with-xslt or how-can-i-merge-these-two-xml-files-using-xslt , but these examples do not handle the text-output and also not handle a static refernece to'default.xml'.
我正在尝试生成由defaults.xml生成的C头文件,该文件由target.xml进行了修改.
I am trying to generate a C headerfile beeing generated from a defaults.xml that gets amended by an target.xml.
我将xsltproc用作xslt处理器,并且希望能够
xslproc merg.xsl target1.xml > target1.h
.
I am using xsltproc as xslt processor and would like to be able to do
xslproc merg.xsl target1.xml > target1.h
.
意味着有一个defaults.xml文件和不同的target.xml文件
Meaning to have one defaults.xml file and different target.xml files
示例defaults.xml:
example defaults.xml:
<?xml version="1.0" encoding="UTF-8"?>
<defaults>
<ConfigParam name="F_SAMPLE_STRING">
<value>{1,0,0}</value>
</ConfigParam>
<ConfigParam name="F_SAMPLE_INT">
<value>40</value>
</ConfigParam>
<ConfigParam name="F_SAMPLE_X">
<value>TRUE_DEF</value>
</ConfigParam>
</defaults>
和一个样本target1.xml
and a sample target1.xml
<?xml version="1.0" encoding="UTF-8"?>
<Override>
<ConfigParam name="F_SAMPLE_STRING">
<value>"hallo"</value>
</ConfigParam>
<ConfigParam name="F_SAMPLE_Y">
<value>TRUE</value>
</ConfigParam>
</Override>
我自己的起始xslt看起来像这样,但是确实缺少合并/修改部分
My own starting xslt looks like this, but does lack the merging/amendment part
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="fileName" select=" 'defaults.xml' " />
<xsl:param name="defaults" select="document($fileName)" />
<xsl:variable name="defaultParams" select="$defaults//ConfigParam" />
<xsl:template match="@* | node() ">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ConfigParam">
#define <xsl:value-of select="@name"/><xsl:text> </xsl:text><xsl:value-of select="value"/> <xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
我看到的其他示例使用静态target.xml或从静态位置使用两个文件(目标/默认值).它们也不输出文本,而是xml. 我是xslt的新手,无法提出良好的合并身份模式. 请帮忙.
The other examples I saw use a static target.xml or use both files (target/defaults) from static locations. They also do not output text but xml. I am new to xslt and can not come up with a good merging identity pattern. Please help.
推荐答案
如果我理解正确,则想这样做:
If I understand correctly, you want to do:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="path-to-defaults" select="'defaults.xml'" />
<xsl:variable name="defaults" select="document($path-to-defaults)/defaults/ConfigParam" />
<xsl:variable name="overrides" select="/Override/ConfigParam" />
<xsl:template match="/">
<xsl:apply-templates select="$defaults[not(@name = $overrides/@name)]" />
<xsl:apply-templates select="$overrides" />
</xsl:template>
<xsl:template match="ConfigParam">
<xsl:text>#define </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="value"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
这假定您将处理target1.xml
文件并指向常量defaults.xml
文件.结果是:
This assumes you will be processing the target1.xml
file and pointing to a constant defaults.xml
file. The result here is:
#define F_SAMPLE_INT 40
#define F_SAMPLE_X TRUE_DEF
#define F_SAMPLE_STRING "hallo"
#define F_SAMPLE_Y TRUE
注意:对于文本输出,您不想使用身份转换模板.
Note: with a text output, you don't want to use the identity transform template.
这篇关于如何使用xslt合并两个xml文件以生成文本输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!