Home > Software design >  ID Sequencing Elements based on attribute value
ID Sequencing Elements based on attribute value

Time:01-17

Please suggest to generate IDs of 'fig type="float"' type elements. Place the text() node in new element by name 'caption'. Unable to group ELEMENT ATTRIBUTE content.

Input XML:

<article>
<fm>
<journalTitle>The j Title</journalTitle>
</fm>
<body>
<sec1>
<p>The first para <fig type="float">This is First float image</fig></p>
<p>THe other text <fig type="inline">This is First Inline image</fig></p>
    <sec2>
    <p>The first para <fig type="float">This is Second float image</fig></p>
    <p>THe other text <fig type="inline">This is First Inline image</fig></p>
        <sec3>
        <p>The first para <fig type="float">This is third float image</fig></p>
        <p>THe other text</p>
        </sec3>
    </sec2>
</sec1>
</body>
</article>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Xml.XPath;

namespace FigSeq1
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument XDoc1 = new XDocument();
            XDoc1 = XDocument.Load(@"D:\XMLs\InImages.xml");
        
            //Grouping only figs of type <fig type="float"> 
            var FigGroup = XDoc1.Descendants()
                .Where(e=>e.Name.ToString()=="fig")
                .Attributes().Where(e1=>(e1.Name.ToString()=="type").ToString()=="float");

            int cnt = 1;

            foreach(XElement ele1 in FigGroup)
            {
                ele1.Add(new XAttribute("ID", "fg"   cnt));
                var EleAll = ele1.Nodes();
                ele1.Nodes().Remove();

                ele1.Add(new XElement("caption", EleAll));
                cnt  ;
            }
            XDoc1.Save(@"D:\XMLs\Result.xml");
        }
    }
}

Required XML:

<article>
  <fm>
    <journalTitle>The j Title</journalTitle>
  </fm>
  <body>
    <sec1>
      <p>The first para <fig type="float" ID="fg1"><caption>This is First float image</caption></fig></p>
      <p>THe other text <fig type="inline">This is First Inline image</fig></p>
      <sec2>
        <p>The first para <fig type="float" ID="fg2"><caption>This is Second float image</caption></fig></p>
        <p>THe other text <fig type="inline">This is First Inline image</fig></p>
        <sec3>
          <p>The first para <fig type="float" ID="fg3"><caption>This is third float image</caption></fig></p>
          <p>THe other text</p>
        </sec3>
      </sec2>
    </sec1>
  </body>
</article>

CodePudding user response:

Below code has an adjustment in filtering the fig xml elements.

Where(e => e.Name == "fig" && (string)e.Attribute("type") == "float"

Also, the child xml elements of the fig element get replaced all together with the caption xml element.

var doc = XDocument.Load(@"D:\XMLs\InImages.xml");

var figures = doc.Descendants()
    .Where(e => e.Name == "fig" && (string)e.Attribute("type") == "float"
    );
var i = 1;
foreach (var figure in figures)
{
    figure.Add(new XAttribute("ID", $"fg{i}"));
    figure.ReplaceNodes(new XElement("caption", figure.Value));
    i  ;
}

doc.Save(@"D:\XMLs\Result.xml");
  • Related