我想使用xslt修改SOAP请求标头中的时间格式

时间:2012-10-12 13:41:16

标签: xslt

我有一个示例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>

有人可以帮忙吗。

提前致谢。

1 个答案:

答案 0 :(得分: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会将它们复制到结果文档中,并将其值更改为所需的值。