1

我有一个场景,当我需要从 server.xml (tomcat) 中取消注释 SSL 配置并且还需要在其中添加几个属性时。当 SSL 配置已经可用但我的模板不适用于刚刚未注释的部分时,我成功添加了两个属性。任何的想法?

<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet type="text/xsl" href="new2.xsl"?>

<Server port="8005" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->

    <Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    <!--NEED TO UNCOMMENT AND ADD ATTRIBUTES IN THE FOLLOWING ELEMENT--> 

    <!--<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" /> -->


    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">         
    -->
    <Engine name="Catalina" defaultHost="localhost">


      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
      </Host>
    </Engine>
  </Service>
</Server>

xslt 紧随其后,取消注释该部分,如果该部分已取消注释,则在其中添加两个属性。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@* | node()" name="Copy">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="@* | node()" mode="insertConnector">
    <xsl:call-template name="Copy" />
  </xsl:template>
<!--If the SSL configuration doesn't exist but availabe in comments, uncomment it-->
  <xsl:template match="comment()[not(../Connector[@scheme = 'https']) and
                                 contains(., 'Connector') and
                                 (contains(., 'scheme=https') or
                                  contains(., scheme='https'))]">

    <xsl:value-of select="." disable-output-escaping="yes"/> <!--This line uncomments the comment-->
  </xsl:template>

  <xsl:template match = "Connector[@scheme = 'https']" name="AddAttributes">
    <Connector keystoreFile="${user.home}/.keystore">
    <xsl:apply-templates select="@* | node()"/>
  </Connector>
  </xsl:template>
</xsl:stylesheet>

请指导我如何在注释部分添加属性或在取消注释后立即在元素中添加属性。以上 XSLT 只是取消注释 SSL 配置部分。谢谢你。

4

2 回答 2

3

您可以通过编写您自己的扩展函数,或通过调用由您的 XSLT 处理器提供的扩展(例如 saxon:parse()(在 XSLT 3.0 中标准化为 parse-xml())将注释的内容传递给 XML 解析器. 或者,按照@JLRiche 的建议,在禁用输出转义的帮助下使用两阶段管道。

于 2013-03-20T09:29:59.150 回答
1

只需在此处添加我的评论作为答案,因为这是您最终采用的方法:

我认为没有办法做到这一点,至少在 XSLT 1.0 中没有。取消注释过程只是将注释的内容输出为非转义文本,但不会将其识别为 XML 数据。您是否可以使用两遍方法,通过 XSLT 运行 XML 一次以取消注释该部分,然后再一次添加注释?

于 2013-03-20T16:07:35.537 回答