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

Convert an attribute into an element

P: n/a
Short version of question ...

An xml file contains the following line ...

<input type="label" text="fasdfad" css_tag="h1" />

The task is to convert it into

<h1>fasdfad</h1>

But to do the conversion in such a way that the possible values for
css_tag do not have to be enumerated in the xsl file.

Something like :

<xsl:template match="input">
<xsl:choose>
<xsl:when test="@type='label'">
<<xsl:value-of select="@css_tag"/>>
<xsl:value-of select="@text"/>
<<xsl:value-of select="@css_tag"/>/>
</xsl:when>
</xsl:choose>
</xsl:template>

This however, isn't well formated XML.

The enumerated solution goes something like :

xsl:template match="input">
<xsl:choose>
<xsl:when test="@type='label'">

<xsl:when test="@css_tag='h1'">
<h1><xsl:value-of select="@text"/></h1>
</xsl:when>

<xsl:when test="@css_tag='h2'">
<h2><xsl:value-of select="@text"/></h2>
</xsl:when>

</xsl:when>
</xsl:choose>
</xsl:template>
Any ideas?
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
GIMME <gi*******************@yahoo.com> wrote:
Short version of question ...

An xml file contains the following line ...

<input type="label" text="fasdfad" css_tag="h1" />

The task is to convert it into

<h1>fasdfad</h1>
I would do something like

start () { # Usage: start tag att=value ...
declare "$@" # sets up 'type', 'text', 'css_tag' variables
if [[ $1 == input && $type == label ]]; then
echo "<$css_tag>$text</$css_tag>"
fi
}

x='<input type="label" text="fasdfad" css_tag="h1" />'
xml -s start "$x"

Ref:
http://freshmeat.net/projects/bashdiff/
http://home.eol.ca/~parkw/index.html#xml
help xml

It's patch to Bash shell with, in this case, an interface to Expat XML
parser. It's main advantage is that you use shell scripting (something
you know already) to cut/splice XML document. XSL applies to only XML,
whereas shell applies to everything that you do on your machine.

But to do the conversion in such a way that the possible values for
css_tag do not have to be enumerated in the xsl file.

Something like :

<xsl:template match="input">
<xsl:choose>
<xsl:when test="@type='label'">
<<xsl:value-of select="@css_tag"/>>
<xsl:value-of select="@text"/>
<<xsl:value-of select="@css_tag"/>/>
</xsl:when>
</xsl:choose>
</xsl:template>

This however, isn't well formated XML.

The enumerated solution goes something like :

xsl:template match="input">
<xsl:choose>
<xsl:when test="@type='label'">

<xsl:when test="@css_tag='h1'">
<h1><xsl:value-of select="@text"/></h1>
</xsl:when>

<xsl:when test="@css_tag='h2'">
<h2><xsl:value-of select="@text"/></h2>
</xsl:when>

</xsl:when>
</xsl:choose>
</xsl:template>
Any ideas?


--
William Park <op**********@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.