XSLT链接或组合xsl:copy-of和xsl:copy

时间:2011-04-12 17:16:17

标签: xslt xsd wsdl extract chaining

我有以下两个XSL转换,我想在一个XSL文件中链接在一起。

第一个转换:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:s0="http://schemas.xmlsoap.org/wsdl/">
<xsl:template match="/">
  <xsl:copy-of select="/s0:definitions/s0:types/xs:schema"/>
</xsl:template>
</xsl:stylesheet>

第二个转换(使用第一个输出作为输入):

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:s0="http://schemas.xmlsoap.org/wsdl/">
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="/xs:schema/xs:element[@name='ServiceAuth']"/>
<xsl:template match="/xs:schema/xs:import[@namespace='http://www.somecompany.com/serviceAuth/ ']"/>
</xsl:stylesheet>

我打算用此完成的目的是从WSDL中仅复制xs:schema节点(及其所有子节点),同时从xs:schema节点中删除两个节点。

如何在一个XSL中将这两个链接在一起,或者是否有更好的方法来实现上述目标?

下面是WSDL输入文件,它应该作为第一个转换的输入:

<s0:definitions name="CBS_GetUpgradeEligibility" targetNamespace="http://www.somecompany.com/" xmlns:s0="http://schemas.xmlsoap.org/wsdl/" xmlns:s1="http://www.somecompany.com/CBS_GetUpgradeEligibility/" xmlns:s2="http://www.somecompany.com/" xmlns:s3="http://schemas.xmlsoap.org/wsdl/soap/">
   <s0:types>
      <xs:schema targetNamespace="http://www.somecompany.com/CBS_GetUpgradeEligibility/" xmlns:auth="http://www.somecompany.com/serviceAuth/" xmlns:cbs="http://www.somecompany.com/CBS_GetUpgradeEligibility/" xmlns:esb="http://www.somecompany.com/esbTypes/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.somecompany.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xs:import namespace="http://www.somecompany.com/esbTypes/" schemaLocation="http://127.0.0.1:5000/CBS_GetUpgradeEligibility?SCHEMA%2FVodacom+Services%2FInfrastructure+Services%2FSchemas%2FXSD_ESBTypes"/>
         <xs:import namespace="http://www.somecompany.com/serviceAuth/ " schemaLocation="http://127.0.0.1:5000/CBS_GetUpgradeEligibility?SCHEMA%2FVodacom+Services%2FInfrastructure+Services%2FSchemas%2FXSD_ServiceAuth"/>
         <xs:simpleType name="DESTINATION_MSISDN">
            <xs:restriction base="xs:string">
               <xs:maxLength value="11"/>
            </xs:restriction>
         </xs:simpleType>
         <xs:simpleType name="STRING_13">
            <xs:restriction base="xs:string">
               <xs:maxLength value="13"/>
            </xs:restriction>
         </xs:simpleType>
         <xs:simpleType name="STRING_20">
            <xs:restriction base="xs:string">
               <xs:maxLength value="20"/>
            </xs:restriction>
         </xs:simpleType>
         <xs:complexType name="UPGRADE_TYPE_DETAILS">
            <xs:sequence>
               <xs:element maxOccurs="1" minOccurs="1" name="isber" type="cbs:STRING_13"/>
               <xs:element maxOccurs="1" minOccurs="1" name="isreward" type="cbs:STRING_13"/>
               <xs:element maxOccurs="1" minOccurs="1" name="code" type="cbs:STRING_20"/>
            </xs:sequence>
         </xs:complexType>
         <xs:complexType name="QUALIFIES_FOR_UPGRADE_DETAILS">
            <xs:sequence>
               <xs:element maxOccurs="1" minOccurs="1" name="isaveragespentmet" type="cbs:STRING_13"/>
               <xs:element maxOccurs="1" minOccurs="1" name="is7daypremature" type="cbs:STRING_13"/>
               <xs:element maxOccurs="1" minOccurs="1" name="upgradepossible" type="cbs:STRING_13"/>
               <xs:element maxOccurs="1" minOccurs="1" name="upgradeduedate" type="cbs:STRING_13"/>
               <xs:element maxOccurs="1" minOccurs="0" name="UpgradeType" type="cbs:UPGRADE_TYPE_DETAILS"/>
            </xs:sequence>
         </xs:complexType>
         <xs:element name="ServiceAuth">
            <xs:complexType>
               <xs:sequence>
                  <xs:element name="Username" type="auth:Username"/>
                  <xs:element name="Password" type="auth:Password"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
         <xs:element name="GetUpgradeEligibilityRequest">
            <xs:complexType>
               <xs:sequence>
                  <xs:element name="TSORequest" type="cbs:getUpgradeEligibilityRequestElement"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
         <xs:element name="GetUpgradeEligibilityResponse">
            <xs:complexType>
               <xs:sequence>
                  <xs:element maxOccurs="1" minOccurs="1" name="TSOID" type="esb:TSOID"/>
                  <xs:element name="TSOResponse" type="cbs:getUpgradeEligibilityResponseElement"/>
                  <xs:element maxOccurs="1" minOccurs="1" name="TSOResult" type="esb:TSOResult"/>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
         <xs:complexType name="getUpgradeEligibilityRequestElement">
            <xs:sequence>
               <xs:element maxOccurs="1" minOccurs="1" name="MSISDN" type="cbs:DESTINATION_MSISDN"/>
            </xs:sequence>
         </xs:complexType>
         <xs:complexType name="getUpgradeEligibilityResponseElement">
            <xs:sequence>
               <xs:element maxOccurs="1" minOccurs="1" name="MSISDN" type="cbs:DESTINATION_MSISDN"/>
               <xs:element maxOccurs="1" minOccurs="1" name="QualifiesForUpgrade" type="cbs:QUALIFIES_FOR_UPGRADE_DETAILS"/>
            </xs:sequence>
         </xs:complexType>
      </xs:schema>
   </s0:types>
   <s0:message name="GetUpgradeEligibilityRequestMessage">
      <s0:part element="s1:ServiceAuth" name="ServiceAuth"/>
      <s0:part element="s1:GetUpgradeEligibilityRequest" name="GetUpgradeEligibilityRequest"/>
   </s0:message>
   <s0:message name="GetUpgradeEligibilityResponseMessage">
      <s0:part element="s1:GetUpgradeEligibilityResponse" name="GetUpgradeEligibilityResponse"/>
   </s0:message>
   <s0:portType name="esbTransactionPort">
      <s0:operation name="getUpgradeEligibility">
         <s0:input message="s2:GetUpgradeEligibilityRequestMessage"/>
         <s0:output message="s2:GetUpgradeEligibilityResponseMessage"/>
      </s0:operation>
   </s0:portType>
   <s0:binding name="getUpgradeEligibilityEsbTransactionBinding" type="s2:esbTransactionPort">
      <s3:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <s0:operation name="getUpgradeEligibility">
         <s3:operation soapAction="" style="document"/>
         <s0:input>
            <s3:header message="s2:GetUpgradeEligibilityRequestMessage" part="ServiceAuth" use="literal"/>
            <s3:body parts="GetUpgradeEligibilityRequest" use="literal"/>
         </s0:input>
         <s0:output>
            <s3:body parts="GetUpgradeEligibilityResponse" use="literal"/>
         </s0:output>
      </s0:operation>
   </s0:binding>
   <s0:service name="getUpgradeEligibilityEsbTransactionBindingQSService">
      <s0:port binding="s2:getUpgradeEligibilityEsbTransactionBinding" name="getUpgradeEligibilityEsbTransactionBindingQSPort">
         <s3:address location="http://VESB14-PRD:7701/CBS_GetUpgradeEligibility"/>
      </s0:port>
   </s0:service>
</s0:definitions>

3 个答案:

答案 0 :(得分:4)

如果你想要第三个样式表组成那两个执行相同转换的样式表:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:s0="http://schemas.xmlsoap.org/wsdl/">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/">
        <xsl:apply-templates select="s0:definitions/s0:types/xs:schema"/>
    </xsl:template>
    <xsl:template match="xs:schema/xs:element[@name='ServiceAuth']"/>
    <xsl:template match="xs:schema/xs:import[@namespace='http://www.somecompany.com/serviceAuth/']"/>
</xsl:stylesheet>

然后你需要:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:import href="stylesheet2.xsl"/>
    <xsl:import href="stylesheet1.xsl"/>
    <xsl:template match="/">
        <xsl:variable name="vFirst">
            <xsl:apply-imports/>
        </xsl:variable>
        <xsl:apply-templates select="msxsl:node-set($vFirst)/node()"/>
    </xsl:template>
</xsl:stylesheet>

输出:

<xs:schema targetNamespace="http://www.somecompany.com/CBS_GetUpgradeEligibility/" xmlns:s0="http://schemas.xmlsoap.org/wsdl/" xmlns:s1="http://www.somecompany.com/CBS_GetUpgradeEligibility/" xmlns:s2="http://www.somecompany.com/" xmlns:s3="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:auth="http://www.somecompany.com/serviceAuth/" xmlns:cbs="http://www.somecompany.com/CBS_GetUpgradeEligibility/" xmlns:esb="http://www.somecompany.com/esbTypes/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.somecompany.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import namespace="http://www.somecompany.com/esbTypes/" schemaLocation="http://127.0.0.1:5000/CBS_GetUpgradeEligibility?SCHEMA%2FVodacom+Services%2FInfrastructure+Services%2FSchemas%2FXSD_ESBTypes"></xs:import>
    <xs:simpleType name="DESTINATION_MSISDN">
        <xs:restriction base="xs:string">
            <xs:maxLength value="11"></xs:maxLength>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="STRING_13">
        <xs:restriction base="xs:string">
            <xs:maxLength value="13"></xs:maxLength>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="STRING_20">
        <xs:restriction base="xs:string">
            <xs:maxLength value="20"></xs:maxLength>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="UPGRADE_TYPE_DETAILS">
        <xs:sequence>
            <xs:element maxOccurs="1" minOccurs="1" name="isber" type="cbs:STRING_13"></xs:element>
            <xs:element maxOccurs="1" minOccurs="1" name="isreward" type="cbs:STRING_13"></xs:element>
            <xs:element maxOccurs="1" minOccurs="1" name="code" type="cbs:STRING_20"></xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="QUALIFIES_FOR_UPGRADE_DETAILS">
        <xs:sequence>
            <xs:element maxOccurs="1" minOccurs="1" name="isaveragespentmet" type="cbs:STRING_13"></xs:element>
            <xs:element maxOccurs="1" minOccurs="1" name="is7daypremature" type="cbs:STRING_13"></xs:element>
            <xs:element maxOccurs="1" minOccurs="1" name="upgradepossible" type="cbs:STRING_13"></xs:element>
            <xs:element maxOccurs="1" minOccurs="1" name="upgradeduedate" type="cbs:STRING_13"></xs:element>
            <xs:element maxOccurs="1" minOccurs="0" name="UpgradeType" type="cbs:UPGRADE_TYPE_DETAILS"></xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="GetUpgradeEligibilityRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="TSORequest" type="cbs:getUpgradeEligibilityRequestElement"></xs:element>
            </xs:sequence>
        </xs:complexType></xs:element>
    <xs:element name="GetUpgradeEligibilityResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="1" minOccurs="1" name="TSOID" type="esb:TSOID"></xs:element>
                <xs:element name="TSOResponse" type="cbs:getUpgradeEligibilityResponseElement"></xs:element>
                <xs:element maxOccurs="1" minOccurs="1" name="TSOResult" type="esb:TSOResult"></xs:element>
            </xs:sequence>
        </xs:complexType></xs:element>
    <xs:complexType name="getUpgradeEligibilityRequestElement">
        <xs:sequence>
            <xs:element maxOccurs="1" minOccurs="1" name="MSISDN" type="cbs:DESTINATION_MSISDN"></xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="getUpgradeEligibilityResponseElement">
        <xs:sequence>
            <xs:element maxOccurs="1" minOccurs="1" name="MSISDN" type="cbs:DESTINATION_MSISDN"></xs:element>
            <xs:element maxOccurs="1" minOccurs="1" name="QualifiesForUpgrade" type="cbs:QUALIFIES_FOR_UPGRADE_DETAILS"></xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

答案 1 :(得分:1)

我已经有一段时间没有接触过XSLT了 - 但我认为你可以迭代地做,而不是通过应用模板。

首先选择/s0:definitions/s0:types/xs:schema - 然后查看其中的所有子项(<xsl:for-each ...如果我没记错的话),并为每个孩子测试它是否是您想要省略的节点。如果不是 - 使用copy-of,否则只需删除它

此外,如果您使用某些Java代码或ANY实用程序运行XSLT,您只需将第一个XSLT的输出作为第二个XSLT的输入传递。

希望这有帮助。

答案 2 :(得分:1)

由于第一个样式表只是选择第二个处理的内容 - 不进行任何转换本身,你不能只是将copy-of更改为apply-templates,并导入第二个样式表吗? (你真的不需要做任何链接。)

<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:s0="http://schemas.xmlsoap.org/wsdl/">

<xsl:import href="stylesheet-2.xsl" />

<xsl:template match="/">
   <xsl:apply-templates select="/s0:definitions/s0:types/xs:schema"/>
</xsl:template>
</xsl:stylesheet>