Convert an attribute into an element

Discussion in 'XML' started by GIMME, Aug 19, 2004.

  1. GIMME

    GIMME Guest

    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?
     
    GIMME, Aug 19, 2004
    #1
    1. Advertising

  2. GIMME

    William Park Guest

    GIMME <> 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 <>
    Open Geometry Consulting, Toronto, Canada
     
    William Park, Aug 19, 2004
    #2
    1. Advertising

  3. > <<xsl:value-of select="@css_tag"/>>
    > <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

    --
    RĂ©mi Peyronnet
     
    =?ISO-8859-1?Q?R=E9mi_Peyronnet?=, Aug 20, 2004
    #3
  4. GIMME

    GIMME Guest

    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.

    > 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.
    >
     
    GIMME, Aug 20, 2004
    #4
  5. GIMME

    Donald Roby Guest

    On Thu, 19 Aug 2004 11:43:17 -0700, GIMME 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>
    >
    > 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.
     
    Donald Roby, Aug 21, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anonymous
    Replies:
    1
    Views:
    5,979
  2. =?Utf-8?B?TWFyaw==?=
    Replies:
    4
    Views:
    635
    =?Utf-8?B?TWFyaw==?=
    Dec 14, 2006
  3. perspolis
    Replies:
    1
    Views:
    2,591
    Mark Rae
    Jan 23, 2007
  4. Joe Kovac
    Replies:
    2
    Views:
    974
    Joe Kovac
    May 2, 2007
  5. CdnRebel
    Replies:
    4
    Views:
    1,980
    CdnRebel via DotNetMonster.com
    Jul 4, 2007
Loading...

Share This Page