如何使用XSLT从XML获取一些信息?

时间:2015-06-26 11:04:35

标签: xml xslt transform xml-namespaces

我想从XML中获取一些字段,这是XML:

<server xmlns="urn:jboss:domain:1.4">
    <extensions>
        ...
    </extensions>
    <management>
        ...
    </management>
    <profile>
        <subsystem xmlns="urn:jboss:domain:logging:1.2">
            ...
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:cmp:1.0"/>
        <subsystem xmlns="urn:jboss:domain:datasources:1.1">
            <datasources>
                <datasource jta="true" jndi-name="java:/calypso/datasources/CalypsoDS" pool-name="CalypsoDS" enabled="true" use-ccm="false">
                    <connection-url>jdbc:oracle:thin:@//tcomora-app.t-maps.local:1521/TIT1CAL.ORA.BDE.ES</connection-url>
                    <driver>oracle</driver>
                    <pool>
                        <min-pool-size>5</min-pool-size>
                        <max-pool-size>200</max-pool-size>
                    </pool>
                    <security>
                        <user-name>TIT1CADM</user-name>
                        <password>TIT1CADM</password>
                    </security>
                    <validation>
                        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker" />
                        <validate-on-match>true</validate-on-match>
                        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter" />
                    </validation>
                    <timeout>
                        <set-tx-query-timeout>true</set-tx-query-timeout>
                        <blocking-timeout-millis>5000</blocking-timeout-millis>
                        <idle-timeout-minutes>15</idle-timeout-minutes>
                    </timeout>
                    <statement>
                        <track-statements>false</track-statements>
                    </statement>
                </datasource>
                <drivers>
                    <driver name="oracle" module="com.oracle.ojdbc">
                        <xa-datasource-class>oracle.jdbc.OracleDriver</xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:deployment-scanner:1.1">
            <deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="60000" auto-deploy-zipped="true" auto-deploy-exploded="true" deployment-timeout="3600"/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:ee:1.1">
            <spec-descriptor-property-replacement>true</spec-descriptor-property-replacement>
            <jboss-descriptor-property-replacement>true</jboss-descriptor-property-replacement>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:ejb3:1.4">
            <session-bean>
                <stateless>
                    <bean-instance-pool-ref pool-name="slsb-strict-max-pool"/>
                </stateless>
                <stateful default-access-timeout="5000" cache-ref="simple"/>
                <singleton default-access-timeout="5000"/>
            </session-bean>
            <mdb>
                <resource-adapter-ref resource-adapter-name="mdb-hornetq-ra"/>
                <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
            </mdb>
            <pools>
                <bean-instance-pools>
                    <strict-max-pool name="slsb-strict-max-pool" max-pool-size="200" instance-acquisition-timeout="10" instance-acquisition-timeout-unit="SECONDS"/>
                    <strict-max-pool name="mdb-strict-max-pool" max-pool-size="50" instance-acquisition-timeout="5" instance-acquisition-timeout-unit="MINUTES"/>
                </bean-instance-pools>
            </pools>
            <caches>
                <cache name="simple" aliases="NoPassivationCache"/>
                <cache name="passivating" passivation-store-ref="file" aliases="SimpleStatefulCache"/>
            </caches>
            <passivation-stores>
                <file-passivation-store name="file"/>
            </passivation-stores>
            <async thread-pool-name="default"/>
            <timer-service thread-pool-name="default">
                <data-store path="timer-service-data" relative-to="jboss.server.data.dir"/>
            </timer-service>
            <remote connector-ref="remoting-connector" thread-pool-name="remoting">
                <channel-creation-options>
                    <option name="WORKER_READ_THREADS" value="8" type="xnio"/>
                    <option name="WORKER_WRITE_THREADS" value="8" type="xnio"/>
                    <option name="MAX_INBOUND_MESSAGES" value="200" type="remoting"/>
                    <option name="MAX_OUTBOUND_MESSAGES" value="200" type="remoting"/>
                </channel-creation-options>                
            </remote>
            <thread-pools>
                <thread-pool name="default">
                    <max-threads count="10"/>
                    <keepalive-time time="100" unit="milliseconds"/>
                </thread-pool>
                <thread-pool name="remoting">
                    <max-threads count="200"/>
                    <keepalive-time time="1000" unit="milliseconds"/>
                </thread-pool>
            </thread-pools>
            <iiop enable-by-default="false" use-qualified-name="false"/>
            <default-security-domain value="other"/>
            <default-missing-method-permissions-deny-access value="false"/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:infinispan:1.4">
            <cache-container name="web" aliases="standard-session-cache" default-cache="local-web" module="org.jboss.as.clustering.web.infinispan">
                <local-cache name="local-web" batching="true">
                    <file-store passivation="false" purge="false"/>
                </local-cache>
            </cache-container>
            <cache-container name="hibernate" default-cache="local-query" module="org.jboss.as.jpa.hibernate:4">
                <local-cache name="entity">
                    <transaction mode="NON_XA"/>
                    <eviction strategy="LRU" max-entries="10000"/>
                    <expiration max-idle="100000"/>
                </local-cache>
                <local-cache name="local-query">
                    <transaction mode="NONE"/>
                    <eviction strategy="LRU" max-entries="10000"/>
                    <expiration max-idle="100000"/>
                </local-cache>
                <local-cache name="timestamps">
                    <transaction mode="NONE"/>
                    <eviction strategy="NONE"/>
                </local-cache>
            </cache-container>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:jacorb:1.3">
            <orb socket-binding="jacorb" ssl-socket-binding="jacorb-ssl">
                <initializers transactions="spec" security="identity"/>
            </orb>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:jaxr:1.1">
            <connection-factory jndi-name="java:jboss/jaxr/ConnectionFactory"/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:jaxrs:1.0"/>
        <subsystem xmlns="urn:jboss:domain:jca:1.1">
            <archive-validation enabled="true" fail-on-error="true" fail-on-warn="false"/>
            <bean-validation enabled="true"/>
            <default-workmanager>
                <short-running-threads>
                    <core-threads count="50"/>
                    <queue-length count="50"/>
                    <max-threads count="50"/>
                    <keepalive-time time="10" unit="seconds"/>
                </short-running-threads>
                <long-running-threads>
                    <core-threads count="50"/>
                    <queue-length count="50"/>
                    <max-threads count="50"/>
                    <keepalive-time time="10" unit="seconds"/>
                </long-running-threads>
            </default-workmanager>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:jdr:1.0"/>
        <subsystem xmlns="urn:jboss:domain:jmx:1.2">
            <expose-resolved-model/>
            <expose-expression-model/>
            <remoting-connector/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:jpa:1.1">
            <jpa default-datasource="" default-extended-persistence-inheritance="DEEP"/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:jsf:1.0"/>
        <subsystem xmlns="urn:jboss:domain:jsr77:1.0"/>
        <subsystem xmlns="urn:jboss:domain:mail:1.0">
            <mail-session jndi-name="java:jboss/mail/Default">
                <smtp-server outbound-socket-binding-ref="mail-smtp"/>
            </mail-session>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:messaging:1.3">
            <hornetq-server>
                <persistence-enabled>true</persistence-enabled>
                <security-enabled>false</security-enabled>
                <jmx-management-enabled>true</jmx-management-enabled>
                <journal-type>ASYNCIO</journal-type>
                <journal-sync-non-transactional>false</journal-sync-non-transactional>
                <journal-file-size>10240000</journal-file-size>
                <journal-min-files>2</journal-min-files>
                <connectors>
                    <netty-connector name="netty" socket-binding="messaging">
                        <param key="use-nio" value="true"/>
                    </netty-connector>
                    <netty-connector name="netty-throughput" socket-binding="messaging-throughput">
                        <param key="batch-delay" value="50"/>
                    </netty-connector>
                    <in-vm-connector name="in-vm" server-id="0"/>
                    <connector name="calypso-jms-connector">
                        <factory-class>org.hornetq.core.remoting.impl.netty.NettyConnectorFactory</factory-class>
                        <param key="host" value="es0tlv06-app.t-maps.local"/>
                        <param key="port" value="5445"/>
                    </connector>
                </connectors>
                <acceptors>
                    <netty-acceptor name="netty" socket-binding="messaging">
                        <param key="direct-deliver" value="false"/>
                        <param key="use-nio" value="true"/>
                    </netty-acceptor>
                    <netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
                        <param key="batch-delay" value="50"/>
                        <param key="direct-deliver" value="false"/>
                    </netty-acceptor>
                    <in-vm-acceptor name="in-vm" server-id="0"/>
                </acceptors>
                <security-settings>
                    <security-setting match="#">
                        <permission type="send" roles="basic_user_role"/>
                        <permission type="consume" roles="basic_user_role"/>
                        <permission type="createNonDurableQueue" roles="basic_user_role"/>
                        <permission type="deleteNonDurableQueue" roles="basic_user_role"/>
                    </security-setting>
                </security-settings>
                <address-settings>
                    <!-- calypso topic -->
                    <address-setting match="jms.topic.calypso">
                        <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                        <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                        <redelivery-delay>0</redelivery-delay>
                        <max-size-bytes>250857600</max-size-bytes>
                        <page-size-bytes>25085760</page-size-bytes>
                        <address-full-policy>PAGE</address-full-policy>
                        <message-counter-history-day-limit>10</message-counter-history-day-limit>
                    </address-setting>
                    <!--default for catch all-->
                    <address-setting match="#">
                        <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                        <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                        <redelivery-delay>0</redelivery-delay>
                        <max-size-bytes>10485760</max-size-bytes>
                        <page-size-bytes>5048576</page-size-bytes>
                        <address-full-policy>PAGE</address-full-policy>
                        <message-counter-history-day-limit>10</message-counter-history-day-limit>
                    </address-setting>
                </address-settings>
                <jms-connection-factories>
                    <connection-factory name="hornetq-ra">
                        <connectors>
                            <connector-ref connector-name="in-vm"/>
                        </connectors>
                        <entries>
                            <entry name="java:/calypso/jms/ConnectionFactory"/>
                        </entries>
                    </connection-factory>
                    <connection-factory name="RemoteConnectionFactory">
                        <connectors>
                            <connector-ref connector-name="netty"/>
                        </connectors>
                        <entries>
                            <entry name="java:jboss/exported/jms/connectionFactory"/>
                        </entries>
                    </connection-factory>
                    <pooled-connection-factory name="mdb-hornetq-ra">
                        <transaction mode="none"/>
                        <connectors>
                            <connector-ref connector-name="in-vm"/>
                        </connectors>
                        <entries>
                            <entry name="java:/calypso/jms/PooledConnectionFactory"/>
                        </entries>
                    </pooled-connection-factory>
                </jms-connection-factories>
                <jms-destinations>
                    <jms-topic name="calypso.adminevents">
                        <entry name="topic/calypsoAdmin"/>
                        <entry name="java:jboss/exported/topic/calypsoAdmin"/>
                    </jms-topic>
                    <jms-topic name="calypso">
                        <entry name="topic/calypso"/>
                        <entry name="java:jboss/exported/topic/calypso"/>
                    </jms-topic>
                    <jms-queue name="CalypsoToBONY">
                      <entry name="queue/CalypsoToBONY"/>
                      <durable>false</durable>
                    </jms-queue>
                    <jms-queue name="BONYToCalypso">
                      <entry name="queue/BONYToCalypso"/>
                      <durable>false</durable>
                    </jms-queue>
                    <jms-queue name="CalypsoToGSCC">
                      <entry name="queue/CalypsoToGSCC"/>
                      <durable>false</durable>
                    </jms-queue>
                    <jms-queue name="GSCCToCalypso">
                      <entry name="queue/GSCCToCalypso"/>
                      <durable>false</durable>
                    </jms-queue>
                    <jms-queue name="CalypsoToSwift">
                      <entry name="queue/CalypsoToSwift"/>
                      <durable>false</durable>
                    </jms-queue>
                    <jms-queue name="SwiftToCalypso">
                      <entry name="queue/SwiftToCalypso"/>
                      <durable>false</durable>
                    </jms-queue>
                    <jms-queue name="DLQ">
                        <entry name="queue/DLQ"/>
                        <durable>false</durable>
                    </jms-queue>
                    <jms-queue name="ExpiryQueue">
                        <entry name="queue/ExpiryQueue"/>
                        <durable>false</durable>
                    </jms-queue>
                </jms-destinations>
            </hornetq-server>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:naming:1.2">
            <remote-naming/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:pojo:1.0"/>
        <subsystem xmlns="urn:jboss:domain:remoting:1.1">
            <connector name="remoting-connector" socket-binding="remoting" security-realm="CalypsoSecurityRealm"/>            
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:resource-adapters:1.0"/>
        <subsystem xmlns="urn:jboss:domain:sar:1.0"/>
        <subsystem xmlns="urn:jboss:domain:security:1.2">
            <security-domains>
                <security-domain name="calypso-db-security-domain" cache-type="default">
                    <authentication>
                        <login-module code="com.calypso.authentication.module.CalypsoAuditLoginModule" flag="required" module="deployment.dataserver.war"/>
                        <login-module code="Database" flag="required">
                            <module-option name="dsJndiName" value="java:/calypso/datasources/CalypsoDS"/>
                            <module-option name="principalsQuery" value="SELECT hex_password from user_name WHERE user_name=? and acc_locked_date is NULL"/>
                            <module-option name="rolesQuery" value="SELECT 'AdmServer','Roles' FROM group_access userrole, group_access usergroup, user_name u WHERE u.user_name=? AND u.user_name=usergroup.access_value AND userrole.group_name = usergroup.group_name AND ( ( userrole.access_id=1 AND userrole.access_value='AdmServer' ) OR ( usergroup.access_id=0 AND usergroup.group_name IN ( SELECT ugn.group_name FROM user_group_name ugn WHERE ugn.is_admin_b=1 or ugn.group_name='admin' ) ) ) UNION SELECT 'basic_user_role', 'Roles' FROM calypso_info"/>
                            <module-option name="hashAlgorithm" value="SHA-1"/>
                            <module-option name="hashCharset" value="UTF-8"/>
                            <module-option name="hashEncoding" value="hex"/>
                        </login-module>
                    </authentication>
                </security-domain>
                <security-domain name="other" cache-type="default">
                    <authentication>
                        <login-module code="Remoting" flag="optional">
                            <module-option name="password-stacking" value="useFirstPass"/>
                        </login-module>
                        <login-module code="RealmDirect" flag="required">
                            <module-option name="password-stacking" value="useFirstPass"/>
                        </login-module>
                    </authentication>
                </security-domain>
                <security-domain name="jboss-web-policy" cache-type="default">
                    <authorization>
                        <policy-module code="Delegating" flag="required"/>
                    </authorization>
                </security-domain>
                <security-domain name="jboss-ejb-policy" cache-type="default">
                    <authorization>
                        <policy-module code="Delegating" flag="required"/>
                    </authorization>
                </security-domain>
            </security-domains>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:threads:1.1"/>
        <subsystem xmlns="urn:jboss:domain:transactions:1.2">
            <core-environment>
                <process-id>
                    <uuid/>
                </process-id>
            </core-environment>
            <recovery-environment socket-binding="txn-recovery-environment" status-socket-binding="txn-status-manager"/>
            <coordinator-environment default-timeout="1800"/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:web:1.4" default-virtual-server="default-host" native="false">
            <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
            <connector name="https" protocol="HTTP/1.1" scheme="https" socket-binding="https" secure="true">
                <ssl name="https" key-alias="calypso" password="maps01" certificate-key-file="/home/apst/mop/key/server.keystore" certificate-file="/home/apst/mop/key/client.truststore"/>
            </connector>
            <connector name="management-https" protocol="HTTP/1.1" scheme="https" secure="true" socket-binding="management-https">
                <ssl name="https" key-alias="calypso" password="maps01" certificate-key-file="/home/apst/mop/key/server.keystore" certificate-file="/home/apst/mop/key/client.truststore"/>
            </connector>
            <virtual-server name="default-host" enable-welcome-root="true">
                <alias name="localhost"/>
            </virtual-server>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:webservices:1.2">
            <modify-wsdl-address>true</modify-wsdl-address>
            <wsdl-host>${jboss.bind.address:127.0.0.1}</wsdl-host>
            <endpoint-config name="Standard-Endpoint-Config"/>
            <endpoint-config name="Recording-Endpoint-Config">
                <pre-handler-chain name="recording-handlers" protocol-bindings="##SOAP11_HTTP ##SOAP11_HTTP_MTOM ##SOAP12_HTTP ##SOAP12_HTTP_MTOM">
                    <handler name="RecordingHandler" class="org.jboss.ws.common.invocation.RecordingServerHandler"/>
                </pre-handler-chain>
            </endpoint-config>
            <client-config name="Standard-Client-Config"/>
        </subsystem>
        <subsystem xmlns="urn:jboss:domain:weld:1.0"/>
    </profile>

我试图执行这个XSL:

<?xml version="1.0" encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


       <xsl:template match="/"> 
                     <xsl:apply-templates/> 
       </xsl:template>


       <xsl:template match="profile">
              <profile>      
                     <xsl:apply-templates select="subsystem"/>
              </profile>
       </xsl:template>

       <xsl:template match="subsystem" >
              <subsystem>
                     <xsl:value-of select="./@xmlns"/>      
                     <xsl:apply-templates select="datasources"/>
              </subsystem>
       </xsl:template>
       <xsl:template match="datasources" >
              <datasources>     
                     <xsl:apply-templates select="datasource"/>
              </datasources>
       </xsl:template>
       <xsl:template match="datasource">
              <datasource>
                     <xsl:value-of select="./@jta"/>
                     <xsl:value-of select="./@jndi-name"/>
                     <xsl:value-of select="./@pool-name"/>
                     <xsl:value-of select="./@enabled"/>
                     <xsl:value-of select="./@use-ccm"/>                    
                     <xsl:apply-templates select="connection-url"/>
                     <xsl:apply-templates select="driver"/>
                     <xsl:apply-templates select="pool"/>
                     <xsl:apply-templates select="security"/>
                     <xsl:apply-templates select="validation"/>
                     <xsl:apply-templates select="statement"/>
              </datasource>
       </xsl:template>       
       <xsl:template match="connection-url">    
              <connection-url>
                     <xsl:value-of select="." />
              </connection-url>
       </xsl:template>       
       <xsl:template match="driver" >     
              <driver>      
                     <xsl:apply-templates select="HostName"/>
              </driver>
       </xsl:template>       
       <xsl:template match="pool">
              <pool>
                     <xsl:apply-templates select="min-pool-size"/>
                     <xsl:apply-templates select="max-pool-size"/>
              </pool>
       </xsl:template>      
       <xsl:template match="security">
              <security>
                     <xsl:apply-templates select="user-name"/>
                     <xsl:apply-templates select="password"/>
              </security>

       </xsl:template>       
       <xsl:template match="validation">
              <validation>
                     <xsl:apply-templates select="valid-connection-checker"/>
                     <xsl:apply-templates select="validate-on-match"/>
                     <xsl:apply-templates select="exception-sorter"/>
              </validation>
       </xsl:template>       
       <xsl:template match="statement">
              <statement>
                     <xsl:apply-templates select="track-statements"/>
              </statement>
       </xsl:template>       
       <xsl:template match="HostName">
              <HostName>
                     <xsl:value-of select="."/>   
              </HostName>
       </xsl:template>      
       <xsl:template match="min-pool-size">
              <min-pool-size>
                     <xsl:value-of select="."/>   
              </min-pool-size>
       </xsl:template>       
       <xsl:template match="max-pool-size">
              <max-pool-size>
                     <xsl:value-of select="."/>   
              </max-pool-size>
       </xsl:template>      
       <xsl:template match="user-name">
              <user-name>
                     <xsl:value-of select="."/>   
              </user-name>
       </xsl:template>      
       <xsl:template match="password">
              <password>
                     <xsl:value-of select="."/>   
              </password>
       </xsl:template>      
       <xsl:template match="valid-connection-checker">
              <valid-connection-checker>

                     <xsl:value-of select="."/>   
              </valid-connection-checker>
       </xsl:template>       
       <xsl:template match="validate-on-match">
              <validate-on-match>

                     <xsl:value-of select="."/>   
              </validate-on-match>
       </xsl:template>       
       <xsl:template match="exception-sorter">
              <exception-sorter>
                     <xsl:value-of select="."/>   
              </exception-sorter>
       </xsl:template>      
       <xsl:template match="track-statements">
              <track-statements>
                     <xsl:value-of select="."/>   
              </track-statements>
       </xsl:template>              
</xsl:stylesheet>

但它不起作用。我只想在“server / profile / subsystem / datasources”

中找到Info

希望有人能有解决方案。 ^^

1 个答案:

答案 0 :(得分:0)

您必须考虑XML中使用的命名空间,并在XSLT中注册它们,如下所示:

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:domain="urn:jboss:domain:1.4"
   xmlns:datasources="urn:jboss:domain:datasources:1.1">

   <xsl:template match="/">
       <xsl:copy-of select="/domain:server/domain:profile/datasources:subsystem/datasources:datasources"/>
   </xsl:template>