Can I Use Variables in Xml Files?

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>

2 Answers

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>
2

Thats what XSLT is for.

XSLT Wiki

Something to get you started:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="" 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest