By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

Help with variable substitution using XSL

P: n/a
I have an XML Document in a format like:

<Variable name="Bob">ABCDEFG</Variable>
<Variable name="Steve">QWERTYUI</Variable>
<Variable name="John">POIUYTR</Variable>
<Variable name="Tim">ZXCVBNM</Variable>

<Function id="1">
<Parameter type="String">Bob</Parameter>
<Parameter type="String">Steve</Parameter>
</Function>

<Function id="2">
<Parameter type="String">John</Parameter>
<Parameter type="String">Tim</Parameter>
</Function>
I am fairly new to doing transformations with XSLT, but so far I
haven't had luck in solving this problem. All I would like to do is
create a new XML output file where the variable declarations are gone,
and the actual values (like ABCDEFG) substituted for the names ("Bob")
for the "parameters." So basically I want the output to be:

<Function id="1">
<Parameter type="String">ABCDEFG</Parameter>
<Parameter type="String">QWERTYUI</Parameter>
</Function>

<Function id="2">
<Parameter type="String">POIUYTR</Parameter>
<Parameter type="String">ZXCVBNM</Parameter>
</Function>

I tried some of the code samples I found on the net regarding find and
replace routines, but I wasn't able to adapt them to this particular
problem.

Thanks in advance!
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


Scott wrote:
I have an XML Document in a format like:

<Variable name="Bob">ABCDEFG</Variable>
<Variable name="Steve">QWERTYUI</Variable>
<Variable name="John">POIUYTR</Variable>
<Variable name="Tim">ZXCVBNM</Variable>

<Function id="1">
<Parameter type="String">Bob</Parameter>
<Parameter type="String">Steve</Parameter>
</Function>

<Function id="2">
<Parameter type="String">John</Parameter>
<Parameter type="String">Tim</Parameter>
</Function>

So basically I want the output to be:

<Function id="1">
<Parameter type="String">ABCDEFG</Parameter>
<Parameter type="String">QWERTYUI</Parameter>
</Function>

<Function id="2">
<Parameter type="String">POIUYTR</Parameter>
<Parameter type="String">ZXCVBNM</Parameter>
</Function>


If I correct your XML to have root element e.g.

<?xml version="1.0" encoding="UTF-8"?>
<root>

<Variable name="Bob">ABCDEFG</Variable>
<Variable name="Steve">QWERTYUI</Variable>
<Variable name="John">POIUYTR</Variable>
<Variable name="Tim">ZXCVBNM</Variable>

<Function id="1">
<Parameter type="String">Bob</Parameter>
<Parameter type="String">Steve</Parameter>
</Function>

<Function id="2">
<Parameter type="String">John</Parameter>
<Parameter type="String">Tim</Parameter>
</Function>

</root>

it is possible with the following XSLT which does start with the
identity transformation and adds two templates, one to make sure the
<Variable> elements are removed and the second to replace the content of
<Parameter> elements as needed:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml"
encoding="UTF-8" />

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="Variable" />

<xsl:template match="Parameter">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:value-of select="/root/Variable[@name = current()]" />
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

It might need some fine-tuning to not copy white space text nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.