0

Is there any way to amend XML by changing or adding an attribute based on nodes siblings/child value?

I need to transform:

<FieldMatchResult FieldName="Record_Amount">

into one of the following:

<FieldMatchResult FieldName="Record_1_Amount">

or

<FieldMatchResult FieldName="Record_Amount" Tag="Record_1_Amount">

here is my sample and I need to pull out value from "Row Index" element

<?xml version="1.0"?>
<ArtifactMatchResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <SubArtifacts>
    <ArtifactMatchResult ArtifactName="Data Record">
      <Fields>
        <FieldMatchResult FieldName="Record_Amount">
          <Values>
            <anyType xsi:type="xsd:double">123456.5</anyType>
          </Values>
        </FieldMatchResult>
        <FieldMatchResult FieldName="Record_Rate" >
          <Values>
            <anyType xsi:type="xsd:double">1.25</anyType>
          </Values>
        </FieldMatchResult>
        <FieldMatchResult FieldName="Row Index">
          <Values>
            <anyType xsi:type="xsd:double">1</anyType>
          </Values>
        </FieldMatchResult>
      </Fields>
      <SubArtifacts />
    </ArtifactMatchResult>
    <ArtifactMatchResult ArtifactName="Data Record">
      <Fields>
        <FieldMatchResult FieldName="Record_Amount">
          <Values>
            <anyType xsi:type="xsd:double">123456.5</anyType>
          </Values>
        </FieldMatchResult>
        <FieldMatchResult FieldName="Record_Rate" >
          <Values>
            <anyType xsi:type="xsd:double">1.25</anyType>
          </Values>
        </FieldMatchResult>
         <FieldMatchResult FieldName="Row Index">
          <Values>
            <anyType xsi:type="xsd:double">2</anyType>
          </Values>
        </FieldMatchResult>
     </Fields>
      <SubArtifacts />
    </ArtifactMatchResult>
  </SubArtifacts>
</ArtifactMatchResult>

Many thanks for any pointers.

1
  • What exactly do you mean by "based on nodes siblings/child value"? Please provide clear criteria: if this, then that, otherwise something else. Commented Oct 19, 2015 at 16:11

1 Answer 1

2

Use the following-sibling axis with the following template:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="XML" omit-xml-declaration="yes"/>
  <xsl:template match="ArtifactMatchResult/SubArtifacts/ArtifactMatchResult/Fields">
    <FieldMatchResult FieldName="Record_Amount">
      <xsl:attribute name="Tag">
        <xsl:value-of select="concat('Record_', */following-sibling::FieldMatchResult[@FieldName = 'Row Index']/Values/anyType, '_Amount')" />
      </xsl:attribute>
    </FieldMatchResult>
  </xsl:template>
</xsl:stylesheet>
  • concat() concatenates two or more strings separated by a comma.
  • * selects all the children of the context node.
  • following-sibling::FieldMatchResult selects all the FieldMatchResult siblings nodes that follow the children of the context node.
  • [@FieldName = 'Row Index'] selects all nodes that have a FieldName attribute of value 'Row Index'.

This XSLT, applied to your XML, gives the following result:

  <FieldMatchResult FieldName="Record_Amount" Tag="Record_1_Amount"/>
  <FieldMatchResult FieldName="Record_Amount" Tag="Record_2_Amount"/>
Sign up to request clarification or add additional context in comments.

1 Comment

I will give it a go later this week thanks for description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.