0

XML:

<root>
 <param name="a">valueOfA</param>
 <param name="b">valueOfB</param>
 <param name="c">valueOfC</param>    
</root>

I need to create param for each param node in xml.

So the expected result:

<xsl:param name="a" select="valueOfA" />
<xsl:param name="b" select="valueOfB" />
<xsl:param name="c" select="valueOfC" />

~ Edit:

Made a mistake, I need an actual xslt param, so it will be usable later in code. fixed above.

~ Edit:

XSLT 1.0 required

~ Edit:

Main issue is to make name of the xsl:param from xml value As below is invalid:

<xsl:param name="{@name}" />

Or variable.

Sergej Popov
  • 2,933
  • 6
  • 36
  • 53
  • If you are making substantial updates to your question, please inform anyone who's already answered - like me :) - by commenting on their answer, so they know to update it. – wst Mar 08 '13 at 17:14

2 Answers2

0
<xsl:template match="root">
  <xsl:copy>
    <xsl:apply-templates select="param"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="param">
  <param name="{ ./@name }" select="{ string(.) }"></param>
</xsl:template>
wst
  • 11,681
  • 1
  • 24
  • 39
  • 1
    That should probably be just `select="{ . }"` – Ian Roberts Mar 07 '13 at 18:10
  • @IanRoberts Yes, it's redundant - just using more verbose XPath to be extra clear to OP. – wst Mar 07 '13 at 19:00
  • I'm not sure `./string()` is valid XPath - `string` is a function, not a node test. – Ian Roberts Mar 07 '13 at 19:13
  • @IanRoberts It's definitely valid XPath 2.0; not sure about 1.0 - that might require `string(.)`. Do you think this is confusing? Just trying to be clear to someone learning XSLT. – wst Mar 07 '13 at 19:21
  • When it's not clear from the question whether the OP wants 1.0 or 2.0 I tend to favour examples that are valid in either, and I'm pretty sure `./string()` isn't valid 1.0 – Ian Roberts Mar 07 '13 at 19:26
  • @IanRoberts Okay, point taken. I just want to emphasize that this is getting converted to a string, even though it's implicit in an AVT. – wst Mar 07 '13 at 19:28
0

From your edits it appears you are looking for a way to use one XSLT to create another. In order to create elements in the xsl: namespace you need to either use <xsl:element> or use the namespace aliasing facility

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
     xmlns:xslo="urn:xsl-output" exclude-result-prefixes="xslo">
  <xsl:namespace-alias stylesheet-prefix="xslo" result-prefix="xsl" />

  <xsl:template match="root">
    <xslo:stylesheet version="1.0">
      <xsl:apply-templates select="param" />
    </xslo:stylesheet>
  </xsl:template>

  <xsl:template match="param">
    <xslo:param name="{@name}" select="{.}" />
  </xsl:template>
</xsl:stylesheet>

Elements prefixed xslo: in the stylesheet become xsl: in the output document (which itself is another stylesheet).

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183