1

I want to set up a parameter entity in order to declare attributes common to multiple elements.

What I am trying to do is make the "elem" element have the attribute "width CDATA "0". After I make that work, I would use it on more elements.

This is the code I came up with:

<?xml version="1.0"?>
<!DOCTYPE example [
    <!ENTITY % attrs 'width   CDATA   "0"'>

    <!ELEMENT example (elem) >
    <!ELEMENT elem (#PCDATA) >
    <!ATTLIST elem %attrs; > <!-- <This errors with "no name for attribute" -->
]>

<example>
    <elem width="20">Hi how are you</elem>
</example>

The code I use is based on the accepted answer to a similar question (How do I declare attributes common to multiple elements?). However, when I run my code through XML validators (Exalt on SublimeText or xmlvalidation.com it errs.

On the site mentioned above, the error is:
The attribute name must be specified in the attribute-list declaration for element "elem1".

With Exalt, the error is:
ATTLIST: no name for Attribute at [...]

What I understand is that the name of the attribute is missing. But I put the name of the attribute on the parameter entity, so how can it not be detected?

If I replace %attrs; with 'width CDATA "0"', it works fine.

Why am I getting the error?

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
edoreld
  • 303
  • 1
  • 17
  • 1
    Great question. +1 Also if the accepted answer in the other question helped, an upvote is appreciated. ;-) – Daniel Haley Apr 05 '19 at 15:49
  • 1
    I've slowly come to realize that to get good answers you need to make the effort to ask good questions. Will spread the voting accordingly and thanks again for your help. – edoreld Apr 05 '19 at 17:04

1 Answers1

1

This is because of restrictions on parameter entities in the internal subset.

One of the restrictions is that parameter entity references can't be used in markup declarations.

From the spec:

Well-formedness constraint: PEs in Internal Subset

In the internal DTD subset, parameter-entity references must not occur within markup declarations; they may occur where markup declarations can occur. (This does not apply to references that occur in external parameter entities or to the external subset.)

If you put your DTD in a separate file, it works fine. (You probably don't need it, but just in case, see here for an example of referencing a DTD in an external file.)

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • 1
    Just realized that the question declares the XML version as 1.0, but my link to the spec is 1.1. It's the same in 1.0; here's the link for reference: https://www.w3.org/TR/REC-xml/#dtd – Daniel Haley Apr 05 '19 at 15:56
  • 1
    Thank you. Putting the DTD content in an external file fixed the problem. – edoreld Apr 05 '19 at 16:18