In the earliest chronicles, Roland is said to have died at the hands of Basques. In later versions, the mythologized battle of Roncesvalles is between him as commander of Charlemagne's rearguard and the Saracens. But in Spain, the story is told slightly differently; the battle is the climax of a legendarium focused around a different character entirely. That story asserts the primacy of an independent Spain, and gained prominence as the notion of a united Spanish realm with its own national identity was coming to the fore. Naturally, therefore, it figures strongly in the romancero viejo.
King Alfonso of Leon - actually Alfonso II of Asturias, called "Alfonso the Chaste" because he took no wife; the Kingdom of Asturias did not change its name to Leon until after his reign - is said to have denied his blessing for the marriage of his sister Ximena to Don Sancho, Count of Saldaña. They were wedded to one another anyway, and so Alfonso ordered his brother-in-law thrown in the dungeon for life and forced his sister to join a convent. Doña Ximena, however, was already pregnant with Don Sancho's son, who was born and given the name Bernardo del Carpio (or, in some versions, Bernaldo). Alfonso raised his nephew as his own foster son and heir, but kept the fact that his father was an imprisoned count a secret from the boy.
As Bernardo grew older, he learned of his own backstory and attempted to persuade Alfonso to release Sancho from his imprisonment, but was unsuccessful.
Alfonso conspired in secret to send a message to Charlemagne asking for the Frankish emperor's assistance in defeating his Moorish enemies; in return, he promised to name Charlemagne his heir, for he had no sons of his own. Charlemagne agreed, but the people of Leon did not; under the leadership of Bernardo del Carpio, many of Alfonso's vassals banded together to resist their proudly independent kingdom being subjugated by a foreign power, even a Christian one. Ultimately, this resistance prompted Charlemagne to leave, and as he was retreating across the Pyrenees, Bernardo and his forces set upon the rearguard. Roland, who in the French stories is often said to have died without ever receiving a blow from his Muslim enemies, is specifically noted in the Spanish legends as having died at Bernardo del Carpio's own hand.
This story is attested back to the high middle ages, but it becomes especially ubiquitous in the sixteenth century. The cycle of Spanish ballads which tell the life history of Bernardo seem to have been quite popular; many texts are known for certain ballads, and at least one balladeer felt the need to compose a brief broadsheet which serves as a kind of prologue; in this ballad, printed first of all the Bernardo ballads in Nucio's cancionero, we hear of Sancho's imprisonment and Ximena's exile to the nunnery after she gives birth to her son, who we are portentiously told is the famous Bernardo del Carpio. As the legend grew in popularity as a celebration of the Spanish nation (at a time when the very idea of celebrating national rather than cultural identity seems to have been taking off in Europe), the seventeenth-century baroque poet Bernardo de Balbuena penned an epic on the subject, entitled, El Bernardo, o, Victoria de Roncesvalles ("Bernardo, or, Victory of Roncesvalles"). The work, which runs for roughly forty thousand lines and is divided into twenty-four books (of which the last tells the story of the battle of Roncesvalles, in a form that accords with Spanish legend), adds a great deal of other folkloric material into the Bernardo cycle and is peppered with genealogies and other dry passages which, in its own day, served to tie it into a grand sense of the glory of Spanish history. It was an instant bestseller.
To the high medieval jongleurs who sang the story of Bernardo at the Spanish court, it posed an ethical dilemma, or perhaps more accurately a dilemma of honor - is it more admirable to fight for one's own birthright, for one's family, for the honor of one's kingdom, or for the decrees of one's king? What if the latter two of these are, by unusual circumstances, directly opposed? Bernardo chooses to fight for the honor of the kingdom and crown of Leon, even when this requires treason against his king - a king who is both his maternal uncle and his foster father. As the idea of the nation-state rose to prominence in the early modern period, the dilemma fell away; Bernardo came to be the man who fought to solidify the independence of the first Christian kingdom in the Iberian peninsula after the Moorish conquest, and so embodied the notion of one unified and sovereign Christian Spain. Despite not being a founder of the kingdom itself, he is identified as its first deliverer and thus as one of the people who built the nation known to Balbuena's readers as the imperial superpower in which they were proud to reside; in this way, his place in the Spanish culture of the era was much like that of Abraham Lincoln to present-day Americans. It is no surprise, then, that the story reaches its greatest and fullest literary expression at the zenith of the Spanish empire, just after a century-long poetic tradition in which the same story serves the same cultural function.