XSLT合并具有相同标识符元素的节点

时间:2016-04-22 17:48:17

标签: xml xslt join merge

在以下示例中,要求是通过移动<userSearchRecords>子元素来合并<userAccount/login>元素(由<otherAttributes>元素标识),如果需要,这意味着:如果有2 <userSearchRecords> xml中的元素,通过将<otherAttributes>从第二个<userSearchRecords>移动到第一个<userSearchRecords>来连接它们,并删除第二个<SearchReply_OutputVariable> <part name="parameters"> <searchResponse> <userSearchRecords> <userAccount> <login>A</login> <otherAttributes> <name>X</name> <value>1</value> </otherAttributes> </userAccount> </userSearchRecords> <userSearchRecords> <userAccount> <login>B</login> <otherAttributes> <name>X</name> <value>2</value> </otherAttributes> </userAccount> </userSearchRecords> <userSearchRecords> <userAccount> <login>C</login> <otherAttributes> <name>X</name> <value>1</value> </otherAttributes> </userAccount> </userSearchRecords> <userSearchRecords> <userAccount> <login>D</login> <otherAttributes> <name>X</name> <value>1</value> </otherAttributes> </userAccount> </userSearchRecords> <userSearchRecords> <userAccount> <login>G</login> <otherAttributes> <name>Y</name> <value>5</value> </otherAttributes> </userAccount> </userSearchRecords> <userSearchRecords> <userAccount> <login>C</login> <otherAttributes> <name>Y</name> <value>6</value> </otherAttributes> </userAccount> </userSearchRecords> <userSearchRecords> <userAccount> <login>H</login> <otherAttributes> <name>Y</name> <value>7</value> </otherAttributes> </userAccount> </userSearchRecords> <userSearchRecords> <userAccount> <login>A</login> <otherAttributes> <name>Y</name> <value>7</value> </otherAttributes> </userAccount> </userSearchRecords> </searchResponse> </part> </SearchReply_OutputVariable> 元素;否则保持原样。

示例输入XML:

<SearchReply_OutputVariable>
  <part name="parameters">
    <searchResponse>
      <userSearchRecords>
        <userAccount>
          <login>A</login>
          <otherAttributes>
            <name>X</name>
            <value>1</value>
          </otherAttributes>
          <otherAttributes>
            <name>Y</name>
            <value>7</value>
          </otherAttributes>
        </userAccount>
      </userSearchRecords>
      <userSearchRecords>
        <userAccount>
          <login>B</login>
          <otherAttributes>
            <name>X</name>
            <value>2</value>
          </otherAttributes>
        </userAccount>
      </userSearchRecords>
      <userSearchRecords>
        <userAccount>
          <login>C</login>
          <otherAttributes>
            <name>X</name>
            <value>1</value>
          </otherAttributes>
          <otherAttributes>
            <name>Y</name>
            <value>6</value>
          </otherAttributes>
        </userAccount>
      </userSearchRecords>
      <userSearchRecords>
        <userAccount>
          <login>D</login>
          <otherAttributes>
            <name>X</name>
            <value>1</value>
          </otherAttributes>
        </userAccount>
      </userSearchRecords>
      <userSearchRecords>
        <userAccount>
          <login>G</login>
          <otherAttributes>
            <name>Y</name>
            <value>5</value>
          </otherAttributes>
        </userAccount>
      </userSearchRecords>
      <userSearchRecords>
        <userAccount>
          <login>H</login>
          <otherAttributes>
            <name>Y</name>
            <value>7</value>
          </otherAttributes>
        </userAccount>
      </userSearchRecords>
    </searchResponse>
  </part>
</SearchReply_OutputVariable>

预期输出XML:

<userSearchRecords>

在输出XML中,现在有6个<userSearchRecords>,而在输入XML中有8个。在输入XML中有2个<login><userSearchRecords>值为A,而2 { {1}} <login>值C.在输出XML中,这些元素被合并(<otherAttributes>子项从匹配的登录对中移出,<userSearchRecords>元素从匹配的对中移除)。

有人能指出我正确的方向,如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

感谢Martin,我查找了与xsl相关的“分组”,这就是我提出的解决方案:

{{1}}