XSLT 增强 Hadoop 配置 [英] XSLT to augment Hadoop config
问题描述
什么是可以根据名称添加或替换属性值的 XSLT(1.0 版)转换?
例如,给定以下输入 XML
<预><代码><配置><财产><name>dfs.replication</name><值>1</值></属性><财产><name>dfs.namenode.name.dir</name><value>/hadoop/dfs/name</value></属性></配置>我将如何指定具有名称和值的两个属性,例如:
<预><代码><配置><财产><name>dfs.replication</name><值>2</值></属性><财产><name>dfs.datanode.data.dir</name><value>/hadoop/dfs/data</value></属性></配置>那么生成的 XML 包含根 configuration
元素的所有原始子元素,并且只有一个具有给定 name
的 property
?例如:
我已经尝试从其他几个问题中举例,但它们没有相同的架构,而且我对 XSLT 的了解不足以适应我的用例.
鉴于:
输入 XML
<预><代码><配置><财产><name>A</name><value>旧A</value></属性><财产><name>B</name><value>旧 B</value></属性></配置>override.xml
<预><代码><配置><财产><name>B</name><value>新B</value></属性><财产><name>C</name><value>new C</value></属性></配置>以下样式表:
XSLT 1.0
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/><xsl:strip-space elements="*"/><xsl:param name="override-path" select="'override.xml'"/><xsl:variable name="override-properties" select="document($override-path)/configuration/property"/><xsl:template match="/configuration"><xsl:copy><!-- 复制未被外部属性覆盖的本地属性--><xsl:copy-of select="property[not(name=$override-properties/name)]"/><!-- 添加所有覆盖属性--><xsl:copy-of select="$override-properties"/></xsl:copy></xsl:模板></xsl:stylesheet>
将返回:
What is an XSLT (version 1.0) transform that can add or replace property values based on name?
For example, given the following input XML
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>/hadoop/dfs/name</value>
</property>
</configuration>
How would I specify two properties with names and values, for example:
<configuration>
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>/hadoop/dfs/data</value>
</property>
</configuration>
So the resulting XML contains all original children of the root configuration
element, and only one property
with a given name
? For example:
<configuration>
<property>
<name>dfs.replication</name>
<value>2</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>/hadoop/dfs/name</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>/hadoop/dfs/data</value>
</property>
</configuration>
I've tried exampled from several other questions, but they don't have the same schema, and I don't know enough XSLT to adjust to my use case.
Given:
Input XML
<configuration>
<property>
<name>A</name>
<value>old A</value>
</property>
<property>
<name>B</name>
<value>old B</value>
</property>
</configuration>
override.xml
<configuration>
<property>
<name>B</name>
<value>new B</value>
</property>
<property>
<name>C</name>
<value>new C</value>
</property>
</configuration>
the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="override-path" select="'override.xml'" />
<xsl:variable name="override-properties" select="document($override-path)/configuration/property" />
<xsl:template match="/configuration">
<xsl:copy>
<!-- copy local properties not overridden by external properties -->
<xsl:copy-of select="property[not(name=$override-properties/name)]"/>
<!-- add all overiding properties -->
<xsl:copy-of select="$override-properties"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
will return:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property>
<name>A</name>
<value>old A</value>
</property>
<property>
<name>B</name>
<value>new B</value>
</property>
<property>
<name>C</name>
<value>new C</value>
</property>
</configuration>
这篇关于XSLT 增强 Hadoop 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!