Web Config Transforms: Insert If Not Exists
Chciałbym zastosować transformację wtedy i tylko wtedy, gdy dopasowany element nie istnieje w obiekcie docelowym. Wypróbowanie różnych wyrażeń xpath przy użyciu http://webconfigtransformationtester.apphb.com ale jak na razie bez powodzenia.
Np. jeśli docelowa siećconfig wygląda tak:
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
Wtedy wyjście powinno wyglądać tak:
<configuration>
<connectionStrings>
<add name="MyCs" provider="System.Data.SqlClient" connectionString="" />
<add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" />
</connectionStrings>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
Ale jeśli cel wygląda tak:
<configuration>
<connectionStrings>
<add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" />
</connectionStrings>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
Wtedy wynik transformacji powinien wyglądać następująco to:
<configuration>
<connectionStrings>
<add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" />
<add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" />
</connectionStrings>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
Innymi słowy, chcę tylko dodać łańcuch nazwanego połączenia do konfiguracji, ale pozwolić administratorowi wypełnić go własnymi wartościami. Myślałem, że będzie to tak proste jak xdt:Transform="Insert" xdt:Locator="XPath(count(/configuration/connectionStrings)=0)"
(dodać sekcję CS config, jeśli nie istnieje), ale najwyraźniej nie.
5 answers
Użyj xdt:Transform="InsertIfMissing"
z XmlTransform
zadaniem w VS2012. Nie wygląda na to, że Microsoft zaktualizował swoją dokumentację, aby odzwierciedlić to jeszcze.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-24 04:01:43
W moim przypadku xdt:Transform="InsertIfMissing"
nie zadziałało Bez xdt:Locator="Match(name)"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-13 02:10:04
Spróbuj tej alternatywnej transformacji dla XDT: Transform= "InsertIfMissing" :
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<nodeToInsertIfMissing xdt:Transform="Insert" />
<nodeToInsertIfMissing xdt:Transform="Remove" xdt:Locator="XPath(/configuration/nodeToInsertIfMissing[2])" />
</configuration>
Powinno działać zgodnie z dokumentacją MSDN :
Insert - dodaje element zdefiniowany w pliku transform jako rodzeństwo do wybranego elementu lub elementów. Nowy element jest dodawany na końcu dowolnej kolekcji.
Tak więc, jeśli węzeł już istnieje, dodajemy drugi, a następnie usuwamy ten węzeł (2nd). W przeciwnym razie dodajemy nowy, unikalny węzeł, ale operacja usunięcia nie powiedzie się.
Uwaga: wydaje się, że nie działa z NuGet *.(un) install.XDT transformacja. InsertIfMissing też.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-12 14:04:20
Potwierdzono działanie w wersji VS2015 i hosta konsoli Menedżera pakietów w wersji 3.4.4.1321 (można to znaleźć po otwarciu konsoli Menedżera pakietów).
To wstawi jeśli 'configuration\connectionStrings \ add\ @ name' nie istnieje.
Aplikacja.config.zainstaluj.xdt:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings xdt:Transform="InsertIfMissing">
<add name="MyCs" provider="System.Data.SqlClient" connectionString="" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
The .plik nuspec:
<files>
<file src="app.config.install.xdt" target="content\app.config.install.xdt" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-12 11:28:47
Użyj xdt:Transform="Remove"
, a następnie xdt:Transform="Insert"
przekształceń. xdt:Transform="InsertIfMissing"
zasugerowany gdzie indziej nie działa dla mnie, wygląda na to, że jego wersja jest specyficzna.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-07 05:59:47