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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.

You Might Also Like