2

我有一个如下的 xml 文件 propNode.xml

<NODES>
  <NODE>
<NODELINE CLASS="Item" TYPE="Item" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName" />
    <ATTR_NODE NAME="myDesc />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item1" TYPE="Item1" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName1" />
    <ATTR_NODE NAME="myDesc1" />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item2" TYPE="Item2" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName2" />
    <ATTR_NODE NAME="myDesc2" />
  </ATTR_NODES>
</NODELINE>
</NODE>
</NODES>

我想读取这个xml并使用它,我需要合并以下两个xml文件 source.xml

<NODES>
  <NODE>
<NODELINE CLASS="Item" TYPE="Item" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName" VALUE="myNameValue" />
    <ATTR_NODE NAME="myDesc" VALUE="test-myDescValue" />
<ATTR_NODE NAME="myId" VALUE="test-myIdValue" />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item1" TYPE="Item1" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName1" VALUE="myNameValue1" />
    <ATTR_NODE NAME="myDesc1" VALUE="myDescValue1"/>
<ATTR_NODE NAME="myId1" VALUE="myIdValue1" />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item2" TYPE="Item2" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName2" VALUE="test-myNameValue2" />
    <ATTR_NODE NAME="myDesc2" VALUE="myDescValue2"/>
<ATTR_NODE NAME="myId2" VALUE="test-myIdValue2" />
  </ATTR_NODES>
</NODELINE>
  </NODE>
</NODES>

target.xml

<NODES>
  <NODE>
<NODELINE CLASS="Item" TYPE="Item" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName" VALUE="myNameValue" />
    <ATTR_NODE NAME="myDesc" VALUE="myDescValue" />
<ATTR_NODE NAME="myId" VALUE="myIdValue" />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item1" TYPE="Item1" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName1" VALUE="myNameValue1" />
    <ATTR_NODE NAME="myDesc1" VALUE="myDescValue1"/>
<ATTR_NODE NAME="myId1" VALUE="myIdValue1" />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item2" TYPE="Item2" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName2" VALUE="myNameValue2" />
    <ATTR_NODE NAME="myDesc2" VALUE="myDescValue2"/>
<ATTR_NODE NAME="myId2" VALUE="myIdValue2" />
  </ATTR_NODES>
</NODELINE>
  </NODE>
</NODES>

条件是读取propNode.xml,如果@NAME的值在source.xml和target.xml中匹配,那么需要比较source.xml和target.xml中@VALUE的值,并输出一个xml创建如下:

期望输出.xml

<NODES>
  <NODE>
<NODELINE CLASS="Item" TYPE="Item" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName" SRCVALUE="myNameValue" TGTVALUE="myNameValue" ISDIFF="false" />
    <ATTR_NODE NAME="myDesc" SRCVALUE="test-myDescValue" TGTVALUE="myDescValue" ISDIFF="true" />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item1" TYPE="Item1" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName1" SRCVALUE="myNameValue1" TGTVALUE="myNameValue1" ISDIFF="false" />
    <ATTR_NODE NAME="myDesc1" SRCVALUE="myDescValue1" TGTVALUE="myDescValue1" ISDIFF="false" />
  </ATTR_NODES>
</NODELINE>
<NODELINE CLASS="Item2" TYPE="Item2" >
  <ATTR_NODES>
    <ATTR_NODE NAME="myName2" SRCVALUE="test-myNameValue2" TGTVALUE="myNameValue2" ISDIFF="true" /> />
    <ATTR_NODE NAME="myDesc2" SRCVALUE="myDescValue2" TGTVALUE="myDescValue2" ISDIFF="false" />
  </ATTR_NODES>
</NODELINE>
  </NODE>
</NODES>

如果在propNode.xml 中选择了@NAME 的值,则desiredOutput.xml 应该包含来自源和目标的@VALUE 的值。如果@VALUE 的值不同,则@ISDIFF 的值应为“true”,否则为“false”。

这整个操作可以使用 xslt 吗?类似于使用 propNode.xml 生成 xsl,然后将其应用于 source.xml 和 target.xml 以生成所需的输出?这个 xsl 会是什么样子?

4

1 回答 1

1

假设只有ATTR_NODE存在的节点 ( )propNode.xml需要测试,下面的 XSLT 应该可以完成这项工作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="some.uri" version="1.0">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ATTR_NODE">
        <xsl:variable name="NodeLine" select="../../@CLASS" />
        <xsl:variable name="AttrName" select="@NAME" />
        <xsl:variable name="SrcValue" select="document('source.xml')//NODELINE[@CLASS=$NodeLine]//ATTR_NODE[@NAME=$AttrName]/@VALUE" />
        <xsl:variable name="TgtValue" select="document('target.xml')//NODELINE[@CLASS=$NodeLine]//ATTR_NODE[@NAME=$AttrName]/@VALUE" />
        <xsl:element name="ATTR_NODE">
            <xsl:attribute name="NAME">
                <xsl:value-of select="$AttrName"/>
            </xsl:attribute>
            <xsl:attribute name="SRCVALUE">
                <xsl:value-of select="$SrcValue" />
            </xsl:attribute>
            <xsl:attribute name="TGTVALUE">
                <xsl:value-of select="$TgtValue" />
            </xsl:attribute>
            <xsl:attribute name="ISDIFF">
                <xsl:value-of select="$SrcValue!=$TgtValue" />
            </xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

标识模板用于遍历和复制,对源和目标中的每个检查propNode进行特殊处理,然后进行相等性评估。我假设这足以确定- 如果不是,那么您还需要添加额外的检查,例如。ATTR_NODEVALUENODE_LINE/@CLASSNODE_LINE@TYPE

xslt 针对 运行propnode.xmlsource.xml并且target.xml必须存在于同一文件夹中。

输出

<?xml version="1.0" encoding="utf-8"?>
<NODES>
    <NODE>
        <NODELINE CLASS="Item" TYPE="Item">
            <ATTR_NODES>
                <ATTR_NODE NAME="myName" SRCVALUE="myNameValue" TGTVALUE="myNameValue" ISDIFF="false" />
                <ATTR_NODE NAME="myDesc" SRCVALUE="test-myDescValue" TGTVALUE="myDescValue" ISDIFF="true" />"
            </ATTR_NODES>
        </NODELINE>
        <NODELINE CLASS="Item1" TYPE="Item1">
            <ATTR_NODES>
                <ATTR_NODE NAME="myName1" SRCVALUE="myNameValue1" TGTVALUE="myNameValue1" ISDIFF="false" />
                <ATTR_NODE NAME="myDesc1" SRCVALUE="myDescValue1" TGTVALUE="myDescValue1" ISDIFF="false" />
            </ATTR_NODES>
        </NODELINE>
        <NODELINE CLASS="Item2" TYPE="Item2">
            <ATTR_NODES>
                <ATTR_NODE NAME="myName2" SRCVALUE="test-myNameValue2" TGTVALUE="myNameValue2" ISDIFF="true" />
                <ATTR_NODE NAME="myDesc2" SRCVALUE="myDescValue2" TGTVALUE="myDescValue2" ISDIFF="false" />
            </ATTR_NODES>
        </NODELINE>
    </NODE>
</NODES>
于 2012-11-27T12:55:32.323 回答