简体   繁体   English

如何使用xslt合并两个xml文件以生成文本输出

[英]How to merge two xml files using xslt generating text output

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'. 我已经在如何与xslt合并两个XML文件如何 与xslt 合并这两个XML文件中看到了我的问题的变体,但是这些示例无法处理文本输出,也不处理对“ default.xml”的静态引用。

I am trying to generate a C headerfile beeing generated from a defaults.xml that gets amended by an target.xml. 我正在尝试生成从defaults.xml生成的C头文件,该文件由target.xml修改。

I am using xsltproc as xslt processor and would like to be able to do xslproc merg.xsl target1.xml > target1.h . 我使用xsltproc作为xslt处理器,并希望能够执行xslproc merg.xsl target1.xml > target1.h

Meaning to have one defaults.xml file and different target.xml files 意味着有一个defaults.xml文件和不同的target.xml文件

example defaults.xml: 示例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> 
and a sample target1.xml 和样本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> 

My own starting xslt looks like this, but does lack the merging/amendment part 我自己的启动xslt看起来像这样,但是确实缺少合并/修改部分

 <?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>&#xd;&#xa;</xsl:text> </xsl:template> </xsl:stylesheet> 

The other examples I saw use a static target.xml or use both files (target/defaults) from static locations. 我看到的其他示例使用静态target.xml或从静态位置使用两个文件(目标/默认值)。 They also do not output text but xml. 它们也不输出文本,而是xml。 I am new to xslt and can not come up with a good merging identity pattern. 我是xslt的新手,无法提出好的合并身份模式。 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>&#xd;&#xa;</xsl:text> 
</xsl:template>  

</xsl:stylesheet>

This assumes you will be processing the target1.xml file and pointing to a constant defaults.xml file. 假设您将处理target1.xml文件并指向一个恒定的defaults.xml文件。 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. 注意:对于文本输出,您不想使用身份转换模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM