When using a的b, we're talking about b, which has been modified by a. When using ab, we're talking about ab. E.g., it's 图书馆 = "library" and not 图书的馆 because 图书馆 is thought of as an independent entity. Determining what is and isn't considered an independent entity requires experience.
Furthermore, once these more mechanical aspects are understood, there are artistic considerations like "it just sounds better this way" or "I want to emphasize something", in addition to aspects of people's preferences (one person prefers one way, another person prefers another way).
A common example of where we omit 的 between nouns is to express "closeness", such as in 你爸 = "your father", 我家小林 = "my family's Xiao Lin", 我国 = "my country" (used in China). There's also cases of prefixes and suffixes, where 的 is not used, like 中文, where 文 is a suffix, and can be used in other words like 英文 and 法文.
For your examples, it will almost always be 中文书 and not 中文的书, not because there's any grammar rules which forbids saying 中文的书, but because we think of 中文书 as an entity in itself. We know this from getting a lot of input, and seeing 中文书 being widely used. Further, we'd write e.g. 中文书的封面 ("the cover of the Chinese book"), and not 中文的书的封面 nor 中文的书封面, because we think of 中文书 as an entity in itself.
It'll be the same for 西班牙语书 = "a Spanish book", although since 西班牙语 is a bit long, maybe people might introduce 西班牙语的书 in order to subsequently talk about 书 (which we know from prior context refers to 西班牙语的书), and avoid repeating the 5-character noun 西班牙语书. (Without the 语, it might be 关于西班牙的书 = "a book about Spain".)
In general, the placement of the 的s indicate how we're expected to parse the nouns. E.g. for three nouns:
- In a的b的c, the reader/listener is indicated to interpret a, b, and c as separate entities, and we're talking about c, e.g. 新冠疫情的防控措施的效果 ("the effects of preventative measures for COVID");
- In ab的c, we think of ab and c are separate entities, and we're talking about c, e.g. 环保产业的发展 ("development of the environment industry");
- In, a的bc we think of a and bc are separate entities, and we're talking about bc, e.g. 妈妈的烤鸡菜谱 ("mother's fried chicken recipe");
- In abc, we think of abc as an individual entity, e.g. 机场行李车 ("airport luggage cart").
(PS. ChatGPT helped generate the examples above.)
The last noun is going to be what's talked about, so e.g. in ab的c we're going to talk about c (and ab modifies c), and in a的bc we're going to talk about bc (and a modifies bc).
Often we try to avoid a的b的c because it's ambiguous, e.g. does 黑色的包里的书 mean "the book in the black bag" (interpreted as (a的b)的c) or "the black book in the bag" (interpreted as a的(b的c))? But sometimes it's necessary to use, especially when a and b are long phrases.
To get good at this, you need lots of input to familiarize yourself with what is and isn't widely used as an independent entity.