diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php
index dc14e3868e92effab2eeb794c61a5bff2482b30a..5b3c2bc9fd30acff9c400941e37beca38159967e 100644
--- a/lib/SimpleSAML/Metadata/SAMLBuilder.php
+++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php
@@ -360,10 +360,14 @@ class SimpleSAML_Metadata_SAMLBuilder {
 				$attributeconsumer->appendChild($t);
 			}
 
+			$nameFormat = $metadata->getString('attributes.NameFormat', SAML2_Const::NAMEFORMAT_UNSPECIFIED);
 
 			foreach ($attributes as $attribute) {
 				$t = $this->createElement('RequestedAttribute');
 				$t->setAttribute('Name', $attribute);
+				if ($nameFormat !== SAML2_Const::NAMEFORMAT_UNSPECIFIED) {
+					$t->setAttribute('NameFormat', $nameFormat);
+				}
 				$attributeconsumer->appendChild($t);
 			}