Question

I am writing my first config file and wanted to know if there was a way that I could set "phonenum = 123456789" once and then use "phonenum" instead of "123456789" everywhere else in my xml file.

For example:

<parameter key="phonenum" value ="123456789" />
<parameter key="urls" value = "/phoneNumbers/123456789/.../,/phoneNumbers/123456789/.../.../.../,/phoneNumbers/123456789/.../.../.../" />

Now in the value of the "urls" key I would like to use "phonenum" instead of the actual number. This way in case someone has to change it they change it only at one spot (where its defined) and not in each url.

Any help would be appreciated! Thanks!

Was it helpful?

Solution

As @keshlam points out, the "built-in" XML way to do this is using entity references. In your DTD you can define

<!ENTITY phonenum ....>

and then in the body of your document you can refer to this as &phonenum;. The actual definition of the phone number can be inline within the DTD, or in a separate file referenced from the DTD.

Many people use this mechanism but it isn't very flexible. There are a couple of drawbacks:

  • Setting the phone number means modifying a file in filestore (or using some fairly complex hacks to redirect the reference within your application)

  • Every operation on the file (e.g. an XSLT transformation) will expand the entity reference, replacing it by an actual phone number. But sometimes you may want it left as an entity reference, e.g. if you only want to change the boilerplate text in the document.

Another approach is to replace your XML document with a "fill-in-the-blanks" XSLT stylesheet. The stylesheet can have a parameter

<xsl:param name="phoneNumber" select="'123456789'"/>

and you can then write the URL as

<parameter key="urls" value = "/phoneNumbers/{$phoneNumber}/.../,/phoneNumbers/{$phoneNumber}/.../.../.../,/phoneNumbers/{$phoneNumber}/.../.../.../" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top