$xml = [xml](Get-Content C:\Users\ritsharma\Desktop\original.xml )
$nodes=$xml.selectnodes("/POWERMART/REPOSITORY/FOLDER/MAPPING/TRANSFORMATION/TRANSFORMFIELD[@NAME="*_DT*" or @NAME="*_NULL*"]")
$nodes -Replace '_NULL','this_is_working'
$nodes -Replace '_DT','this_is_working'
$xml.save("C:\Users\ritsharma\Desktop\original.xml")

Tutaj wiercam do węzła dziecka "Transformfield", który ma atrybut "Nazwa". Chcę zastąpić wartość atrybutu "Nazwa" posiadające wartości "lub" _dt "z" To działa ". Ten kod działa bez błędów, ale nie zapewnia żadnej mocy. Zmiany nie można zobaczyć w pliku.

Oto plik XML

<POWERMART>
<REPOSITORY NAME="">
<FOLDER NAME="SA_Test" GROUP="" OWNER="AD" SHARED="NOTSHARED" DESCRIPTION="" 
 PERMISSIONS="rwx------" UUID="86fcaa4c-e96d-4eea-8263-0ee22273ee23">
    <EXPRMACRO DESCRIPTION ="" EXPRESSION ="IIF( NULL_INPUT =&apos;?&apos; ,NULL,TO_DATE( DATE_INPUT,&apos;MM/DD/YYYY&apos;))" MACROTYPE ="Public" NAME ="Convert_Dates" OBJECTVERSION ="1" PROTOTYPE ="DATE/TIME Convert_Dates( NULL_INPUT as string, DATE_INPUT as string )" RETURNTYPE ="date/time" VERSIONNUMBER ="1">
    <MACROARGUMENT ARGORDER ="1" DATATYPE ="string" NAME ="NULL_INPUT" PRECISION ="1" SCALE ="0"/>
    <MACROARGUMENT ARGORDER ="2" DATATYPE ="string" NAME ="DATE_INPUT" PRECISION ="10" SCALE ="0"/>
</EXPRMACRO>
<MAPPING DESCRIPTION ="" ISVALID ="YES" NAME ="M_demo_1_onetoone" OBJECTVERSION ="1" VERSIONNUMBER ="1">
    <TRANSFORMATION DESCRIPTION ="" NAME ="EXP_escrowfile" OBJECTVERSION ="1" REUSABLE ="NO" TYPE ="Expression" VERSIONNUMBER ="1">
        <TRANSFORMFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="Demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="1" SCALE ="0"/>
        <TRANSFORMFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="13" SCALE ="0"/>
        <TRANSFORMFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="11" SCALE ="2"/>
        <TRANSFORMFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="15" SCALE ="2"/>
        <TRANSFORMFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="3" SCALE ="2"/>
        <TRANSFORMFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="1" SCALE ="0"/>
        <TRANSFORMFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="15" SCALE ="4"/>
        <TRANSFORMFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="demo" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="11" SCALE ="2"/>
        <TRANSFORMFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="ST_CHG_DT" EXPRESSIONTYPE ="GENERAL" NAME ="ST_BCB_DT" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="10" SCALE ="0"/>
        <TRANSFORMFIELD DATATYPE ="string" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="ST_CHG_NULL" EXPRESSIONTYPE ="GENERAL" NAME ="ES_DISB_ST_CHG_NULL" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="1" SCALE ="0"/>
        <TRANSFORMFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="ES_E_PST_DUE_30_BA" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="15" SCALE ="2"/>
        <TRANSFORMFIELD DATATYPE ="decimal" DEFAULTVALUE ="" DESCRIPTION ="" EXPRESSION ="demo" EXPRESSIONTYPE ="GENERAL" NAME ="ES_E_PST_DUE_60_BA" PICTURETEXT ="" PORTTYPE ="INPUT/OUTPUT" PRECISION ="15" SCALE ="2"/>
</TRANSFORMATION>
 </MAPPING>
-2
ritwik sharma 25 lipiec 2020, 12:26

1 odpowiedź

Najlepsza odpowiedź

Jednym ze sposobów, aby użyć notacji dot znaleźć węzły zainteresowania, pętlą i zmienić atrybut NAME:

[xml]$xml = Get-Content 'C:\Users\ritsharma\Desktop\original.xml' -Raw

# unlike XPath, dot-notation works case-insensitive
$xml.POWERMART.REPOSITORY.FOLDER.MAPPING.TRANSFORMATION.TRANSFORMFIELD | 
    Where-Object {$_.NAME -match '_(DT|NULL)'} | 
    ForEach-Object {
        $_.SetAttribute('NAME', $_.NAME.Replace($matches[1], 'this_is_working'))
    }

$xml.Save('C:\Users\ritsharma\Desktop\updated_original.xml')

Opublikowany XML jest nieprawidłowy, ponieważ brakuje kilku tagów zamykających.

Powinno być:

<POWERMART>
  <REPOSITORY NAME="">
    <FOLDER NAME="SA_Test" GROUP="" OWNER="AD" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx------" UUID="86fcaa4c-e96d-4eea-8263-0ee22273ee23">
      <EXPRMACRO DESCRIPTION="" EXPRESSION="IIF( NULL_INPUT ='?' ,NULL,TO_DATE( DATE_INPUT,'MM/DD/YYYY'))" MACROTYPE="Public" NAME="Convert_Dates" OBJECTVERSION="1" PROTOTYPE="DATE/TIME Convert_Dates( NULL_INPUT as string, DATE_INPUT as string )" RETURNTYPE="date/time" VERSIONNUMBER="1">
        <MACROARGUMENT ARGORDER="1" DATATYPE="string" NAME="NULL_INPUT" PRECISION="1" SCALE="0" />
        <MACROARGUMENT ARGORDER="2" DATATYPE="string" NAME="DATE_INPUT" PRECISION="10" SCALE="0" />
      </EXPRMACRO>
      <MAPPING DESCRIPTION="" ISVALID="YES" NAME="M_demo_1_onetoone" OBJECTVERSION="1" VERSIONNUMBER="1">
        <TRANSFORMATION DESCRIPTION="" NAME="EXP_escrowfile" OBJECTVERSION="1" REUSABLE="NO" TYPE="Expression" VERSIONNUMBER="1">
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="Demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="1" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="13" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="11" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="3" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="1" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="4" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="11" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="ST_CHG_DT" EXPRESSIONTYPE="GENERAL" NAME="ST_BCB_DT" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="10" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="ST_CHG_NULL" EXPRESSIONTYPE="GENERAL" NAME="ES_DISB_ST_CHG_NULL" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="1" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="ES_E_PST_DUE_30_BA" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="ES_E_PST_DUE_60_BA" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="2" />
        </TRANSFORMATION>
      </MAPPING>
    </FOLDER>
  </REPOSITORY>
</POWERMART>

Korzystanie z kodu w mojej odpowiedzi dałoby Ci nowy XML o nazwie updated_original.xml wygląda:

<POWERMART>
  <REPOSITORY NAME="">
    <FOLDER NAME="SA_Test" GROUP="" OWNER="AD" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx------" UUID="86fcaa4c-e96d-4eea-8263-0ee22273ee23">
      <EXPRMACRO DESCRIPTION="" EXPRESSION="IIF( NULL_INPUT ='?' ,NULL,TO_DATE( DATE_INPUT,'MM/DD/YYYY'))" MACROTYPE="Public" NAME="Convert_Dates" OBJECTVERSION="1" PROTOTYPE="DATE/TIME Convert_Dates( NULL_INPUT as string, DATE_INPUT as string )" RETURNTYPE="date/time" VERSIONNUMBER="1">
        <MACROARGUMENT ARGORDER="1" DATATYPE="string" NAME="NULL_INPUT" PRECISION="1" SCALE="0" />
        <MACROARGUMENT ARGORDER="2" DATATYPE="string" NAME="DATE_INPUT" PRECISION="10" SCALE="0" />
      </EXPRMACRO>
      <MAPPING DESCRIPTION="" ISVALID="YES" NAME="M_demo_1_onetoone" OBJECTVERSION="1" VERSIONNUMBER="1">
        <TRANSFORMATION DESCRIPTION="" NAME="EXP_escrowfile" OBJECTVERSION="1" REUSABLE="NO" TYPE="Expression" VERSIONNUMBER="1">
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="Demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="1" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="13" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="11" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="3" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="1" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="4" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="demo" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="11" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="ST_CHG_DT" EXPRESSIONTYPE="GENERAL" NAME="ST_BCB_this_is_working" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="10" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="string" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="ST_CHG_NULL" EXPRESSIONTYPE="GENERAL" NAME="ES_DISB_ST_CHG_this_is_working" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="1" SCALE="0" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="ES_E_PST_DUE_30_BA" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="2" />
          <TRANSFORMFIELD DATATYPE="decimal" DEFAULTVALUE="" DESCRIPTION="" EXPRESSION="demo" EXPRESSIONTYPE="GENERAL" NAME="ES_E_PST_DUE_60_BA" PICTURETEXT="" PORTTYPE="INPUT/OUTPUT" PRECISION="15" SCALE="2" />
        </TRANSFORMATION>
      </MAPPING>
    </FOLDER>
  </REPOSITORY>
</POWERMART>

Jak widać, dwa <TRANSFORMFIELD> atrybuty nazw zostały zaktualizowane.

0
Theo 25 lipiec 2020, 19:49