2

我有一个示例 SOAP 请求,我想修改收到的 SOAP 请求的安全标头中的时间戳。这是我的示例 SOAP 请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:dat="http://schemas.datacontract.org/2004/07/DataContracts">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsu:Timestamp wsu:Id="Timestamp-78496158-6fa4-41da-8887-b8116829f1d8" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsu:Created>2012-10-12T10:08:42Z</wsu:Created>
<wsu:Expires>2012-10-12T11:13:42Z</wsu:Expires>
</wsu:Timestamp>
<wsse:BinarySecurityToken wsu:Id="SecurityToken-db63adf4-d7c7-4827-8c36-56f672e6c397" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
xyz</wsse:BinarySecurityToken>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
xyz</Signature>
</wsse:Security>
</soapenv:Header>

我想使用 xslt 修改以下元素的内容

<wsu:Created>2012-10-12T10:08:42Z</wsu:Created>
<wsu:Expires>2012-10-12T11:13:42Z</wsu:Expires>

<wsu:Created>2012-10-12T10:08:42.000Z</wsu:Created>
<wsu:Expires>2012-10-12T11:13:42.000Z</wsu:Expires>

有人可以帮忙吗。

提前致谢。

4

1 回答 1

1

这是一个简单、简洁的 XSLT 1.0 解决方案。

当这个 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
  version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" />
  <xsl:strip-space elements="*" />

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

  <xsl:template match="wsu:Created|wsu:Expires">
    <xsl:copy>
      <xsl:value-of select="concat(substring-before(., 'Z'), '.000Z')" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

...应用于最初提供的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:dat="http://schemas.datacontract.org/2004/07/DataContracts" xmlns:tem="http://tempuri.org/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
      <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="Timestamp-78496158-6fa4-41da-8887-b8116829f1d8">
        <wsu:Created>2012-10-12T10:08:42Z</wsu:Created>
        <wsu:Expires>2012-10-12T11:13:42Z</wsu:Expires>
      </wsu:Timestamp>
      <wsse:BinarySecurityToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-db63adf4-d7c7-4827-8c36-56f672e6c397" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3">xyz</wsse:BinarySecurityToken>
      <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">xyz</Signature>
    </wsse:Security>
  </soapenv:Header>
</soapenv:Envelope>

...产生了想要的结果:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:dat="http://schemas.datacontract.org/2004/07/DataContracts" xmlns:tem="http://tempuri.org/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
      <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="Timestamp-78496158-6fa4-41da-8887-b8116829f1d8">
        <wsu:Created>2012-10-12T10:08:42.000Z</wsu:Created>
        <wsu:Expires>2012-10-12T11:13:42.000Z</wsu:Expires>
      </wsu:Timestamp>
      <wsse:BinarySecurityToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-db63adf4-d7c7-4827-8c36-56f672e6c397" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3">xyz</wsse:BinarySecurityToken>
      <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">xyz</Signature>
    </wsse:Security>
  </soapenv:Header>
</soapenv:Envelope>

解释:

  • 模板 #1 是Identity Template. 其目的是将源模板中的所有节点和属性原样复制到结果模板中。
  • 模板 #2 匹配<wsu:Created><wsu:Expires>元素。找到这些元素后,XSLT 将它们复制到结果文档中并将它们的值更改为所需的值。
于 2012-10-12T14:15:43.620 回答