Convert an attribute into an element

G

GIMME

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?
 
W

William Park

GIMME said:
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.
 
?

=?ISO-8859-1?Q?R=E9mi_Peyronnet?=

<xsl:value-of select="@text"/>
<<xsl:value-of select="@css_tag"/>/>
This however, isn't well formated XML.

A correct XSLT solution is to use <xsl:element name="{}"> :

<xsl:template match="input">
<xsl:if test="@type='label'">
<xsl:element name="{@css_tag}"><xsl:value-of
select="@text"/></xsl:element>
</xsl:if>
</xsl:template>

However, the bashdiff solution is quite impressive :)

Hth
 
G

GIMME

Wow!

Thanks William.

You've got the bash magic touch. A useful clip worthy of study ...

But I needed a solution using an xsl transform.
 
D

Donald Roby

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?

You probably want to use xsl:element, getting the element names from your
css_tag attributes.

Something along the lines of

<xsl:template match="input">
<xsl:element name="{@css_tag}">
<xsl:value-of select="@text" />
</xsl:element>
</xsl:template>

I haven't tested this code, but I know I've done something similar for
exactly this purpose.

It does as it stands depend on css_tag always being present and having a
value that makes sense as a tag, so it's not perfectly robust.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top