16

Is it possible to define variables in an XML file?

For example:

VARIABLE = 'CHIEF_NAME'

    <foods>
      <food>
        <name>French Toast</name>
        <price>$4.50</price>
        <calories>600</calories>
        <chief>VARIABLE</chief>
      </food>
      <food>
        <name>Homestyle Breakfast</name>
        <price>$6.95</price>
        <calories>950</calories>
        <chief>VARIABLE</chief>
      </food>
    </foods>
David Makogon
  • 69,407
  • 21
  • 141
  • 189
cbr
  • 437
  • 2
  • 5
  • 16

2 Answers2

16

You can declare an entity reference for chief and reference it as &chief;:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE foods [
  <!ENTITY chief "CHIEF_NAME!">
  <!-- .... -->
]>
<foods>
  <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <calories>600</calories>
    <chief>&chief;</chief>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <calories>950</calories>
    <chief>&chief;</chief>
  </food>
</foods>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • How do you change this for the dynamic values coming from an API. – surazzarus May 11 '18 at 10:26
  • XML provides the above notion of setting a "variable" (entity) value in one location and using it in multiple places. It does not provide the notion of then opening *that* setting to an API of any sort; your application would have to re-write the entity value directly or indirectly through system entity. – kjhughes May 11 '18 at 13:39
0

Thats what XSLT is for.

XSLT Wiki

Something to get you started:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="yourVar" select="'CHIEF_NAME'">
</xsl:variable>
<xsl:template match="/">
  <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <calories>600</calories>
    <chief><xsl:copy-of select="$yourVar" /></chief>
  </food>
</xsl:template>

</xsl:stylesheet>

This syntax is not exactly correct, but I think generally this is the direction you should seek

Jared
  • 310
  • 1
  • 9