How to preserve whitespace within an element's opening tag in XSLT

只谈情不闲聊 提交于 2019-12-02 02:25:55

问题


In order to avoid committing sensitive information to an SCM repository, I wrote an XSL stylesheet that removes connection string passwords from ASP.NET Web.config files. It achieves my goal of removing passwords, but it also affects the whitespace within the opening tags of elements. I would like to preserve this whitespace if possible.

For example, given this Web.config:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="Host=localhost;Username=dev;Password='sensitive password';Database=database"
         providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <!-- ... -->

I am able to transform it to:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="Host=localhost;Username=dev;Password=********;Database=database" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <!-- ... -->

But I would like to transform it to:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="Host=localhost;Username=dev;Password=********;Database=database"
         providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <!-- ... -->

Is this possible?


回答1:


whitespace within tags is not significant and can be normalised by the processor to single spaces and you cannot control this in standard tools. Nor can you control the order of attributes or the characters used for quoting (quot vs apos). Character entities may also be converted to equivalent representations. It is possible that you can find custom serializers that will do want you want but not in XSLT.

Note that the whitespace within element content can be partially preserved if required (but I don't think this was part of your requirement).

See also: Preserving attribute whitespace

And see the very extensive XSLT FAQ on whitespace: http://www.dpawson.co.uk/xsl/sect2/N8321.html



来源:https://stackoverflow.com/questions/4617993/how-to-preserve-whitespace-within-an-elements-opening-tag-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!