2

I have an XSL file, which acts as a configuration file for my application. In fact it is an XML file, which has the <xsl:Stylesheet> elements wrapped around it. This file is called Config.xsl:

     <?xml version="1.0" encoding="UTF-8"?>
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"           xmlns="http://www.example.org/Config">
     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"               standalone="yes" />
     <xsl:template match="/">
     <Config>
          <Test>somevalue</Test>
          <Test1>someothervalue</Test1>
     </Config>
     </xsl:template>
</xsl:stylesheet>

Now I would like to change the value of an element, which is passed dynamically. Meaning, I have another XML file, which contains the XPATH and the value as key name/value pairs. Below is the contents of XML file Properties.xml.

<?xml version="1.0" encoding="utf-8" ?>
<ConfigFiles>
<ConfigFile>
    <FileName>Config.xsl</FileName>
    <Keys>
        <Key Name="Config.Test" Value="newvalue" />
        <Key Name="Config.Test1" Value="newvalue1" />
    </Keys>
</ConfigFile>   
</ConfigFiles>

Below is my powershell code, which is not updating the values of the elements.

$properties = [xml] (Get-Content Properties.xml)
$lstfiles = $properties.ConfigFiles.ConfigFile
foreach($file in $lstfiles)
{
  $configfilename = $file.FileName
  $filename = "C:\configs\configfilename"
  $testconfigfile = [xml] (Get-Content $filename)

  $lstKeys = $file.Keys.Key
  foreach($key in $lstKeys)
  {
    #When I debug the code, I was able to assign the value using the below code (Commented). However this is not dynamic
    #$testconfigfile.DocumentElement.LastChild.Config.Test = "newvalue"

    #Now if I try to pass the same values dynamically by reading them from properties.xml and assigning it using the below code it does not work
    $testconfigfile.DocumentElement.LastChild.$key.Name = $key.Value            
  }
  $testconfigfile.Save($filename)               
}
1
  • $testconfigfile.DocumentElement.LastChild.$key looks suspicious to me. It would help if you clarified your original intent. Maybe give some examples of how it should work. Commented Oct 27, 2012 at 1:06

2 Answers 2

3

This should work for you. Change the properties.xml to

<?xml version="1.0" encoding="utf-8" ?>
<ConfigFiles>
<ConfigFile>
    <FileName>Config.xsl</FileName>
    <Keys>
        <Key Name="Test" Value="newvalue" />
        <Key Name="Test1" Value="newvalue1" />
    </Keys>
</ConfigFile>   
</ConfigFiles>

and try the below line in your shell script ,

$testconfigfile.DocumentElement.LastChild.Config.($key.Name) = $key.Value
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Sir. That works, however it only solves part of the problem. If my XSL (Config.xsl in this case) contains nested elements then it does not work. I checked your alternate solution, but it is not dynamic and for each element I might end up adding multiple attributes (ex:- Name1, Name2...NameN). Also, instead of hardcoding the value "Config" in the script, I am extracting the filename without extension and passing it dynamically. $testconfigfile.DocumentElement.LastChild.$filenamewithoutextension.($key.Name) = $key.Value
1
Alternatively...


<?xml version="1.0" encoding="utf-8" ?>
<ConfigFiles>
<ConfigFile>
    <FileName>Config.xsl</FileName>
    <Keys>
        <Key Name1="Config"  Name2='Test' Value="newvalue" />
        <Key Name1="Config"  Name2='Test1' Value="newvalue1" />
    </Keys>
</ConfigFile>   
</ConfigFiles>


$testconfigfile.DocumentElement.LastChild.($key.Name1).($key.Name2) = $key.Value 

Comments

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.