magic-chunks / magic-chunks-dotnetcore

Easy to use tool to config transformations for JSON, XML and YAML.
MIT License
106 stars 48 forks source link

Inline JSON: how to pick up two attributes with same name? #50

Open zhulingfang opened 6 years ago

zhulingfang commented 6 years ago

I am trying to use Inline JSON to modify web.config file. In this file, I have two host under hosts: hosts host hostname="http://localhost:2469" host hostname="http://localhost:6299/" I am not able to pick up those two hostname to edit using xpath. Got following errors:

[error]System.Management.Automation.MethodInvocationException: Exception calling "Transform" with "4" argument(s): "The '[' character, hexadecimal value 0x5B, cannot be included in a name." ---> System.Xml.XmlException: The '[' character, hexadecimal value 0x5B, cannot be included in a name.

Is there anyway to address these two attributes or is this a bug? Need help on this.

Thanks!

Sigman32 commented 6 years ago

If you could provide your inline trasformation JSON it would be easier to help you out.

But if your web.config is for exapmle:

<configuration>
... <!-- skipped other definitions -->
<hosts>
  <host hostname="http://localhost:2469"/>
  <host hostname="http://localhost:6299/"/>
</hosts>
... <!-- skipped other definitions -->
</configuration>

To replace these two hosts with some other values you could use this Inline JSON transformation:

{
"configuration/hosts/host[0]/@hostname":"http://first.hostname.local/",
"configuration/hosts/host[1]/@hostname":"http://second.hostname.local/"
}

Which should transform your configuration to:

<configuration>
... <!-- skipped other definitions -->
<hosts>
  <host hostname="http://first.hostname.local/"/>
  <host hostname="http://second.hostname.local/"/>
</hosts>
... <!-- skipped other definitions -->
</configuration>
DarrinRobertson commented 5 years ago

How would you add those two entries? I tried unsuccessfully with

{ "configuration/hosts/host[]0/@hostname":"http://first.hostname.local/", "configuration/hosts/host[]1/@hostname":"http://second.hostname.local/" }

DarrinRobertson commented 5 years ago

Actually, I was wrong, that did work.