1

I have troubles with a replacement values in a XML documents.

I have these two pieces of xml:

XmlFile_1

<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72B3-8" Name="     ">../Metadata/A_short_autobiography_of_Herculino_Alves.imdi</CorpusLink>
<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72B5-E" Name=" tttttt    ">../Metadata/Wordlist_and_phrases_-_modifiers.imdi</CorpusLink>

<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72B9-6" Name=" hhhhhh    ">../desano-silva-0151/Metadata/The_Turtle_and_the_Deer.imdi</CorpusLink>
<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72BB-1" Name=" jjjjjj    ">../desano-silva-0151/Metadata/Wordlist_and_phrases_parts_of_a_tree.imdi</CorpusLink>
<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72BD-B" Name=" kkkkk    ">../desano-silva-0151/Metadata/Wordlist_and_phrases_.imdi</CorpusLink>

XMLFile2

<Session>
  <Name>des2008_002_ab</Name>

I need to replace the value of each "CorpusLink.Name" in Xmlfile_1 for the value on Session.Name in XmlFile_2;XmlFile_3;XmlFile_4;XmlFile_5;XmlFile_5 once that I have several xml documents like XmlFile_2.

Basically, the idea is to replace the values like this

xmldoc1.Corpuslink.Name = xmldoc2.Session.Name
xmldoc1.Corpuslink.Name = xmldoc3.Session.Name                  
xmldoc1.Corpuslink.Name = xmldoc4.Session.Name
xmldoc1.Corpuslink.Name = xmldoc5.Session.Name                       
...

For now I have this code:

$Targfiles = Get-ChildItem C:\WorkingFolder\ *.xml
$TrgContent = [xml](Get-Content $Targfiles)

$Sourcfiles = New-Object System.Collections.ArrayList;
$Sourcfiles = Get-ChildItem C:\WorkingFolder\Source\*.xml
$SrcContent = Foreach ($item in $Sourcfiles){[xml] (Get-Content $item)}

$TrgContent.METATRANSCRIPT.Corpus.CorpusLink | ForEach-Object {[String]$_.Name= Foreach ($element in $SrcContent)    {$element.METATRANSCRIPT.Corpus.CorpusLink}}

$OutputFile=$Targfiles.Name
$TrgContent.Save("C:\WorkingFolder\output\$OutputFile")

The code is working but I can't get the result that i need , this is my problem:

$TrgContent.METATRANSCRIPT.Corpus.CorpusLink | ForEach-Object {[String]$_.Name= Foreach ($element in $SrcContent){$element.METATRANSCRIPT.Corpus.CorpusLink}}

RESULT:

<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72B3-8" Name="     ">../Metadata/A_short_autobiography_of_Herculino_Alves.imdi</CorpusLink>
<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72B5-E" Name="     ">../Metadata/Wordlist_and_phrases_-_modifiers.imdi</CorpusLink>
<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72B7-9" Name="     ">../desano-silva-0151/Metadata/Wordlist_fruits_and_cultural_items.imdi</CorpusLink>

<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72B9-6" Name="     ">../desano-silva-0151/Metadata/The_Turtle_and_the_Deer.imdi</CorpusLink>

<CorpusLink ArchiveHandle="hdl:2196/00-0000-0000-000F-72BD-B" Name="     ">../desano-silva-0151/Metadata/Wordlist_and_phrases_.imdi</CorpusLink>

The values in CorpusLink.Name are empty. How can I solve this issue?

Thanks in advance

1 Answer 1

1

You are assigning the value from the same tag an the string typecasting would also not require. Try below.

$i = 0
$TrgContent.METATRANSCRIPT.Corpus.CorpusLink | ForEach-Object {
    $_.Name= $SrcContent[$i].Session.Name.'#text'
    $i++
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help! This Working fine.

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.