2

I have a sample soap request and I want to modify the timestamp in the security header of the SOAP request received. Here is my sample SOAP request

<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>

I want to modify the contents of following elements using xslt

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

to

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

Can someone help please.

Thanks in advance.

Luck888
  • 27
  • 4
  • Out of interest, AFAIK, the millisecond part of [xs:datetime](http://www.w3.org/TR/xmlschema-2/#dateTime) is optional, so why the need for padding zeroes on milliseconds? – StuartLC Oct 12 '12 at 14:30

1 Answers1

1

Here is a simple, concise XSLT 1.0 solution.

When this 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>

...is applied to the originally provided 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>

...the wanted result is produced:

<?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>

Explanation:

  • Template #1 is the Identity Template. It's purpose is to copy all nodes and attributes from the source template to the result template as-is.
  • Template #2 matches both <wsu:Created> and <wsu:Expires> elements. Upon finding these elements, the XSLT copies them to the result document and alters their values to be the desired values.
ABach
  • 3,743
  • 5
  • 25
  • 33
  • @Luck888 - you are welcome. Would you please consider marking this answer as correct by checking the checkmark at the top of the answer? – ABach Oct 12 '12 at 19:41