1

我正在尝试使用 XSLT 更改此 XML SOAP 消息中用于测试的属性的值:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
  <env:Header>
    <wsa:To>http://10.141.160.82:8221/EchoWS/HelloWorldPort</wsa:To>
    <wsa:Action>http://ws.test.support.oracle.com/HelloWorld/sayHelloRequest</wsa:Action>
    <wsa:MessageID>urn:6B291C103CAA11E3BF2AB9016B2C3D3A</wsa:MessageID>
    <wsa:RelatesTo>urn:6B291C103CAA11E3BF2AB9016B2C3D3A</wsa:RelatesTo>
    <wsa:ReplyTo>
      <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
      <wsa:ReferenceParameters>
        <instra:tracking.ecid xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">863df39044fd7fd4:-419f3903:141cd7e2379:-8000-00000000000011e3</instra:tracking.ecid>
        <instra:tracking.conversationId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">urn:6B291C103CAA11E3BF2AB9016B2C3D3A</instra:tracking.conversationId>
        <instra:tracking.parentComponentInstanceId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">reference:3</instra:tracking.parentComponentInstanceId>
        <instra:tracking.compositeInstanceCreatedTime xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">2013-10-24T08:47:19.112-04:00</instra:tracking.compositeInstanceCreatedTime>
      </wsa:ReferenceParameters>
    </wsa:ReplyTo>
    <wsa:FaultTo>
      <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
      <wsa:ReferenceParameters>
        <instra:tracking.ecid xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">863df39044fd7fd4:-419f3903:141cd7e2379:-8000-00000000000011e3</instra:tracking.ecid>
        <instra:tracking.conversationId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">urn:6B291C103CAA11E3BF2AB9016B2C3D3A</instra:tracking.conversationId>
        <instra:tracking.parentComponentInstanceId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">reference:3</instra:tracking.parentComponentInstanceId>
        <instra:tracking.compositeInstanceCreatedTime xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">2013-10-24T08:47:19.112-04:00</instra:tracking.compositeInstanceCreatedTime>
      </wsa:ReferenceParameters>
    </wsa:FaultTo>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" env:mustUnderstand="1">
      <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-wDovd4kFRR1RutxItzNNHw22">
        <wsse:Username>weblogic</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">manager03</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </env:Header>
  <env:Body>
    <sayHello xmlns="http://ws.test.support.oracle.com/">
      <arg0 xmlns="">Miguel</arg0>
    </sayHello>
  </env:Body>
</env:Envelope>

从这里我需要改变的唯一部分是:

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" env:mustUnderstand="1">

至:

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" env:mustUnderstand="0">

这是更改env:mustUnderstand="1""0".

我一直在努力,但一直没能做到。你能帮我用一个我可以用来做这个的 XSLT 吗?

4

2 回答 2

1

使用 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

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

  <xsl:template match="*[local-name()='Security']">
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" env:mustUnderstand="xxxxx">
        <xsl:copy-of select="child::*"/>
    </wsse:Security>
  </xsl:template>
</xsl:stylesheet>

获得输出:

<?xml version="1.0" encoding="utf-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
  <env:Header>
    <wsa:To>http://10.141.160.82:8221/EchoWS/HelloWorldPort</wsa:To>
    <wsa:Action>http://ws.test.support.oracle.com/HelloWorld/sayHelloRequest</wsa:Action>
    <wsa:MessageID>urn:6B291C103CAA11E3BF2AB9016B2C3D3A</wsa:MessageID>
    <wsa:RelatesTo>urn:6B291C103CAA11E3BF2AB9016B2C3D3A</wsa:RelatesTo>
    <wsa:ReplyTo>
      <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
      <wsa:ReferenceParameters>
        <instra:tracking.ecid xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">863df39044fd7fd4:-419f3903:141cd7e2379:-8000-00000000000011e3</instra:tracking.ecid>
        <instra:tracking.conversationId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">urn:6B291C103CAA11E3BF2AB9016B2C3D3A</instra:tracking.conversationId>
        <instra:tracking.parentComponentInstanceId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">reference:3</instra:tracking.parentComponentInstanceId>
        <instra:tracking.compositeInstanceCreatedTime xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">2013-10-24T08:47:19.112-04:00</instra:tracking.compositeInstanceCreatedTime>
      </wsa:ReferenceParameters>
    </wsa:ReplyTo>
    <wsa:FaultTo>
      <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
      <wsa:ReferenceParameters>
        <instra:tracking.ecid xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">863df39044fd7fd4:-419f3903:141cd7e2379:-8000-00000000000011e3</instra:tracking.ecid>
        <instra:tracking.conversationId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">urn:6B291C103CAA11E3BF2AB9016B2C3D3A</instra:tracking.conversationId>
        <instra:tracking.parentComponentInstanceId xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">reference:3</instra:tracking.parentComponentInstanceId>
        <instra:tracking.compositeInstanceCreatedTime xmlns:instra="http://xmlns.oracle.com/sca/tracking/1.0">2013-10-24T08:47:19.112-04:00</instra:tracking.compositeInstanceCreatedTime>
      </wsa:ReferenceParameters>
    </wsa:FaultTo>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" env:mustUnderstand="xxxxx"><wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-wDovd4kFRR1RutxItzNNHw22">
        <wsse:Username>weblogic</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">manager03</wsse:Password>
      </wsse:UsernameToken></wsse:Security>
  </env:Header>
  <env:Body>
    <sayHello xmlns="http://ws.test.support.oracle.com/">
      <arg0 xmlns="">Miguel</arg0>
    </sayHello>
  </env:Body>
</env:Envelope>
于 2013-10-28T12:18:22.347 回答
0

XSLT 中此类问题的标准方法是定义一个身份模板,它将从输入到输出的所有内容都原封不动地复制,除非被更具体的模板覆盖,然后为您想要更改的内容定义此类模板。在这种情况下,您唯一要更改的是一个属性。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

  <!-- match env:mustUnderstand attribute only on the Security header -->
  <xsl:template match="w:Security/@env:mustUnderstand"
        xmlns:w="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
        xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <!-- and replace it with env:mustUnderstand="0" -->
    <xsl:attribute name="env:mustUnderstand">0</xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

在这种情况下,我认为您不需要检查特定的“1”值,但如果您确实需要检查它(例如,如果您想将“1”更改为“0”但不理会其他值)那么你可以使用

match="w:Security/@env:mustUnderstand[. = '1']"
于 2013-10-28T13:23:46.277 回答