cbr
cbr

Reputation: 457

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>

Upvotes: 18

Views: 80923

Answers (2)

kjhughes
kjhughes

Reputation: 111756

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>

Upvotes: 18

Jared
Jared

Reputation: 310

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

Upvotes: 0

Related Questions