如何编写XSLT转换来更新.csdef文件

时间:2013-02-05 11:01:37

标签: xml xslt

我有一个天蓝色的服务定义文件,我需要通过在末尾添加"physicalDirectory"值来更新一些where @ph != 'EmptyFolder'属性('\Deploy\')。

我认为查询的xPath's会像这样

//x:Site[@physicalDirectory != 'EmptyFolder']/@physicalDirectory
//x:VirtualApplication/@physicalDirectory

所以最终结果如下:

<vApp name="main" physicalDirectory="..\blahblahblah\Deploy\" />
你可以帮助我吗?

这是一个示例文件

<ServiceDefinition name="Platform.Services.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2012-05.1.7">
  <WorkerRole name="WorkflowService.Worker" vmsize="ExtraSmall">
    <Imports>
      <Import moduleName="Diagnostics" />
      <Import moduleName="RemoteAccess" />
    </Imports>
    <Certificates>
      <Certificate name="ThreeSeventyCertificate" storeLocation="LocalMachine" storeName="My" />
    </Certificates>
  </WorkerRole>
  <WebRole name="WebService.Web" vmsize="ExtraSmall" enableNativeCodeExecution="true">
     <Sites>
      <Site name="Web" physicalDirectory="EmptyFolder">
        <VirtualDirectory name="api" physicalDirectory="EmptyFolder">
          <VirtualApplication name="main" physicalDirectory="..\Platform.Services.AccountService.Web\" />
          <VirtualApplication name="v1.0" physicalDirectory="..\..\_releases\V1.0\Platform.Services.AccountService.Web\" />
        </VirtualDirectory>
        <VirtualDirectory name="docs" physicalDirectory="EmptyFolder">
          <VirtualApplication name="main" physicalDirectory="..\AccountService.Documentation\" />
          <VirtualApplication name="v1.0" physicalDirectory="..\..\_releases\V1.0\AccountService.Documentation\" />
        </VirtualDirectory>
        <VirtualApplication name="reports" physicalDirectory="..\Platform.Services.ReportService.Web\" />
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
      <Site name="internal" physicalDirectory="..\InternalService.Web\">
        <Bindings>
          <Binding name="internal" endpointName="internal" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="https" port="443" certificate="ThreeSeventy" />
      <InternalEndpoint name="internal" protocol="http">
      </InternalEndpoint>
    </Endpoints>
    <Imports>
      <Import moduleName="Diagnostics" />
      <Import moduleName="RemoteAccess" />
    </Imports>
    <Certificates>
      <Certificate name="ThreeSeventy" storeLocation="LocalMachine" storeName="My" />
    </Certificates>
    <ConfigurationSettings>
      <Setting name="TransportServiceEntities" />
      <Setting name="ReportServiceEntities" />
      <Setting name="AccountServiceEntities" />
      <Setting name="ThreeSeventyEntitiesMembership" />
    </ConfigurationSettings>
  </WebRole>
  <WorkerRole name="ChannelService.Worker" vmsize="ExtraSmall">
    <Imports>
      <Import moduleName="Diagnostics" />
      <Import moduleName="RemoteAccess" />
      <Import moduleName="RemoteForwarder" />
    </Imports>
    <ConfigurationSettings>
      <Setting name="EngineEnabled" />
      <Setting name="PlatFormAPIConnectionString" />
    </ConfigurationSettings>
    <Certificates>
      <Certificate name="ThreeSeventyCertificate" storeLocation="LocalMachine" storeName="My" />
    </Certificates>
  </WorkerRole>
</ServiceDefinition>

1 个答案:

答案 0 :(得分:1)

这是怎么回事:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@physicalDirectory[. != 'EmptyFolder']">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="concat(., 'Deploy\')"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

在样本输入上运行时,值为“EmptyFolder”的physicalDirectory属性将保持不变:

  <Site name="Web" physicalDirectory="EmptyFolder">
    <VirtualDirectory name="api" physicalDirectory="EmptyFolder">

所有其他人都将“部署”附加到最后:

  <VirtualApplication name="main" physicalDirectory="..\Platform.Services.AccountService.Web\Deploy\" />
  <VirtualApplication name="v1.0" physicalDirectory="..\..\_releases\V1.0\Platform.Services.AccountService.Web\Deploy\" />