XSLT生成不需要的命名空间

时间:2018-05-03 10:50:22

标签: xml xslt

我有一个像这样的xml:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd           http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd           http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd          http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd">
<cm:property-placeholder persistent-id="pot_kodtar_service" update-strategy="reload"/>
...
<bean class="xxxxx.fuse.util.CommonAuthInterceptor" id="authorizationInterceptor">
    <property name="methodRolesMap">
        <map>
            <entry key="xxxElemE1" value="xxxElemE1Role"/>
            <entry key="xxxTipusE1" value="xxxTipusE1Role"/>
            <entry key="xxxLekerdezE1" value="xxxLekerdezE1Role"/>
            <entry key="xxxValtozasE1" value="xxxValtozasE1Role"/>
        </map>
    </property>
    <property name="globalRoles" value="xxxUsers"/>
</bean>

我想在最后一个条目后附加一个新元素。如果我发现没有命名空间的最后一个元素如下:

    <xsl:template match="*:map/*:entry[last()]">     
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>       
        <entry key="{$service-name}" value="{$service-name}Role"/>
    </xsl:copy>
</xsl:template>

我在输出文档中得到了额外的名字,如下所示:

               <entry key="xxxValtozasE1" value="xxxValtozasE1Role"/> <!-- that is last existing element-->
        <entry xmlns=""
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               key="xxxElemKeresE1"
               value="xxxElemKeresE1Role"/> <!-- that is newly added element with unwanted namespaces-->

插入的条目包含额外的xmlns:=&#34;&#34;和xmlns:xsd =&#34; ...&#34;定义。如何消除这些额外的命名空间定义?

THX ZAMEK

3 个答案:

答案 0 :(得分:0)

在样式表的<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:tds="http://www.onvif.org/ver10/device/wsdl"> <SOAP-ENV:Body> <tds:GetSystemDateAndTime/> </SOAP-ENV:Body> </SOAP-ENV:Envelope> Response to device.GetSystemDateAndTime <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:tt="http://www.onvif.org/ver10/schema" xmlns:tds="http://www.onvif.org/ver10/device/wsdl"> <SOAP-ENV:Body> <tds:GetSystemDateAndTimeResponse> <tds:SystemDateAndTime> <tt:DateTimeType>NTP</tt:DateTimeType> <tt:DaylightSavings>true</tt:DaylightSavings> <tt:TimeZone> <tt:TZ>CET-1CEST,M3.5.0,M10.5.0</tt:TZ> </tt:TimeZone> <tt:UTCDateTime> <tt:Time> <tt:Hour>15</tt:Hour> <tt:Minute>52</tt:Minute> <tt:Second>25</tt:Second> </tt:Time> <tt:Date> <tt:Year>2010</tt:Year> <tt:Month>10</tt:Month> <tt:Day>29</tt:Day> </tt:Date> </tt:UTCDateTime> <tt:LocalDateTime> <tt:Time> <tt:Hour>17</tt:Hour> <tt:Minute>52</tt:Minute> <tt:Second>25</tt:Second> </tt:Time> <tt:Date> <tt:Year>2010</tt:Year> <tt:Month>10</tt:Month> <tt:Day>29</tt:Day> </tt:Date> </tt:LocalDateTime> </tds:SystemDateAndTime> </tds:GetSystemDateAndTimeResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"根元素上添加exclude-result-prefixes="xsd"xsl:stylesheet。第一个建议应该将结果元素放在输出命名空间中,第二个建议应该避免xsl:transform的输出,我想你已经在样式表中声明了它。

答案 1 :(得分:0)

使用XSLT和命名空间的一般规则是:确保使用正确的扩展名生成元素,并且命名空间声明将自行处理。

您想要生成一个具有扩展名称的元素(&#34; http://www.osgi.org/xmlns/blueprint/v1.0.0&#34;,&#34;条目&#34;),但您实际上已经生成了一个具有扩展名称的元素(&# 34;&#34;,&#34;条目&#34;)。

将文字结果元素更改为

<entry key="{$service-name}" value="{$service-name}Role" xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"/> 

因此该元素现在具有正确的扩展名称,并且不需要的命名空间未声明将消失。

答案 2 :(得分:0)

xslt根目录中的exclude-result-prefixes =“xsd”似乎对于其中一个元素很有用,它在xml内部就像这样:/ bean / property / map / entry。不幸的是,我在根目录下有一个名为route的其他元素:

        <route id="xxxModositE1_Route" streamCache="true">
        <from id="xxxModositE1_from1" uri="cxf:bean:xxxModositE1_LocalEndpoint?dataFormat=PAYLOAD"/>
        <convertBodyTo
            id="xxxModositE1_convertBodyTo1" type="java.lang.String"/>
        <wireTap id="xxxModositE1_wireTap1" uri="direct-vm:logRequest"/>
        <to id="xxxModositE1_to2" uri="cxf:bean:xxxModositE1_RemoteEndpoint?dataFormat=PAYLOAD"/>
        <convertBodyTo
            id="xxxModositE1_convertBodyTo3" type="java.lang.String"/>
        <wireTap id="xxxModositE1_wireTap3" uri="direct-vm:logResponse"/>
    </route>

我尝试将其与:

匹配
 <xsl:template match="*:route[last()]">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>       
    </xsl:copy>
    <xsl:comment><xsl:value-of select='$service-name'/></xsl:comment>
    <route id="{$service-name}_xslt_Route" streamCache="true" xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
        <from id="{$service-name}_from1" uri="cxf:bean:{$service-name}_LocalEndpoint?dataFormat=PAYLOAD"/>
        <convertBodyTo id="{$service-name}_convertBodyTo1" type="java.lang.String"/>
        <wireTap id="{$service-name}_wireTap1" uri="direct-vm:logRequest"/>
        <to id="{$service-name}_to2" uri="cxf:bean:{$service-name}_RemoteEndpoint?dataFormat=PAYLOAD"/>
        <convertBodyTo id="{$service-name}_convertBodyTo3" type="java.lang.String"/>
        <wireTap id="{$service-name}_wireTap3" uri="direct-vm:logResponse"/>
    </route>
    <!--  -->
</xsl:template>

,结果将是:

 <route xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
         id="TorzsHorizontKomplexKodtarElemKeresE1_xslt_Route"
         streamCache="true">
...

当然我不想在这个元素中添加额外的xmlns。该职位是否相关?这是我的xslt的根:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
exclude-result-prefixes="xsd">

THX ZAMEK