Dot-Net
我可以使用 Web Deploy 將元素插入到 web.config 中嗎?
是否可以使用 Web Deploy 的Parameters.xml 系統將 XML 元素插入到我的 web.config 中?
XmlFile參數“ kind ”似乎最接近我的需要,但它的
match屬性只接受 XPath 查詢,而且我似乎無法在我的 XPath 查詢中指定不存在的元素。(或者更確切地說,我可以指定一個不存在的元素 - Web Deploy 只是忽略它。)具體來說,我想轉換它:<configuration> <plugins> <add name="bleh"/> </plugins> </configuration>進入這個:
<configuration> <plugins> <add name="bleh"> <option name="foo" value="bar"/> </add> </plugins> </configuration>(不幸的是,我不能在 web.config 中預先儲存一個空
option元素,因為這個特定的外掛系統不喜歡無法辨識/空的選項。)感謝您的任何想法!
從 Web Deploy V3 開始,這些事情現在成為可能。見官方文件。
下面是一個 parameters.xml 文件的範例,它將添加 newNode 到所有節點,包括目標 xml 文件中的根:
<parameters> <parameter name="Additive" description="Add a node" defaultValue="<newNode />" tags=""> <parameterEntry kind="XmlFile" scope=".*" match="//*" /> </parameter> </parameters>
Xpath 只是一種用於 XML 文件的查詢語言——它本身不能更改 XML 文件或創建新的 XML 文件。
專門為轉換 XML 文件而設計的語言稱為 XSLT。
這是一個非常簡短的 XSLT 轉換,可以解決您的問題:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="add[@name='bleh']"> <xsl:copy> <xsl:copy-of select="@*"/> <option name="foo" value="bar"/> </xsl:copy> </xsl:template> </xsl:stylesheet>當此轉換應用於提供的 XML 文件時:
<configuration> <plugins> <add name="bleh"/> </plugins> </configuration>產生了想要的正確結果:
<configuration> <plugins> <add name="bleh"> <option name="foo" value="bar"/> </add> </plugins> </configuration>