Christophe Debove
Christophe Debove

Reputation: 6286

Xslt transformation

I want to transform with xsl:

<?xml version="1.0" encoding="utf-16"?>
<root>
<value1>test1</value1>
<value2>test2</value2>
<value3>test3</value3>
</root>

in :

<input type="hidden" value="test1" name="value1" />
<input type="hidden" value="test2" name="value2" />
<input type="hidden" value="test3" name="value3" />

What is the best way to do that?

Thank you

Upvotes: 0

Views: 133

Answers (2)

Flynn1179
Flynn1179

Reputation: 12075

You XML's badly designed; if possible, instead of <value1> you should use <value name="value1"> or something similar.

That having been said, you can do:

<xsl:template match="*[starts-with(name(),'value')]">
  <xsl:element name="input">
    <xsl:attribute name="type">hidden</xsl:attribute>
    <xsl:attribute name="value">
      <xsl:value-of select="." />
     </xsl:attribute>
    <xsl:attribute name="name">
      <xsl:value-of select="name()" />
    </xsl:attribute>
  </xsl:element>
</xsl:template>

If you elements were <value name="value1"> instead, the match becomes simply match="value", which is much faster, and the name attribute becomes <xsl:value-of select="@name">.

Upvotes: 1

user357812
user357812

Reputation:

I think this rule is semantically clear and sucint:

<xsl:template match="*[starts-with(name(),'value')]">
    <input type="hidden" value="{.}" name="{name()}" /> 
</xsl:template> 

Upvotes: 4

Related Questions