如何从现有文件中收集特定属性并合并到 linux 中的另一个文件

how to collect a particular attribute from a existing file and merge to another file in linux

谁能告诉我如何使用 xslt 读取 xml 属性进行文件比较。

简要说明

旧 xml 中有 <transportReceiver> 个标签,我需要全部阅读并更新到新 xml 。

<transportReceiver name="http"                  class="org.apache.axis2.transport.http.AxisServletListener">
    <parameter name="port">8080</parameter>        
</transportReceiver>

基本上我想阅读 <transportReceiver> 标签并制作一个集合并附加到新的 axis2.xml 文件。为此,我正在使用 xslt。我为此创建了一个 xsl 文件。

    <?xml version="1.0" encoding="utf-8"?>
<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:variable name="axis2.xml" select="document('axis2.xml')" />



<xsl:template match="/transportReceiver">

</xsl:template>

<xsl:template match="object">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

我需要一些帮助来收集 <transportReceiver>

以下是实现这一目标的方法。让我们假设 First.xml 文件包含 <transportReceiver> 节点以及其他节点。

First.xml

<root>
    <otherNode>
        <otherNodeValue>XXXX</otherNodeValue>
    </otherNode>
    <someOtherNode>
        <someOtherNodeValue>YYYY</someOtherNodeValue>
    </someOtherNode>
    <transportReceiver name="http"  class="org.apache.axis2.transport.http.AxisServletListener">
        <parameter name="port">8080</parameter>
    </transportReceiver>
</root>

Second.xml<transportReceiver>节点需要与其现有节点合并的地方。

Second.xml

<rootNode>
    <axisNode>
        <axisValue>AXIS</axisValue>
    </axisNode>
</rootNode>

下面的 XSLT 应用于 Second.xml 时会生成所需的输出。

XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:param name="fileName" select="document('First.xml')" />
    <!-- identity transform -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="rootNode">
        <xsl:copy>
            <!-- copy required node from First.xml -->
            <xsl:apply-templates select="$fileName/root/transportReceiver" />
            <!-- retain existing nodes of Second.xml as is -->
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出

<rootNode>
    <transportReceiver name="http" class="org.apache.axis2.transport.http.AxisServletListener">
        <parameter name="port">8080</parameter>
    </transportReceiver>
    <axisNode>
        <axisValue>AXIS</axisValue>
    </axisNode>
</rootNode>