Jump to content

Using attributes for xsl:copy-of?


lithium

Recommended Posts

Scenario:I have XML files wich look like this:...<foo> <bar1></something></bar1> <bar2></something_different></bar2> <bar3></again_something_different></bar3></foo><foo> <bar1></something></bar> <bar2></something_different></bar2> <bar3></again_something_different></bar3></foo>...so there are many instances of <foo> with different data stored in them.What I want to have is this:...<foo name="1"> <bar1></something></bar1> <bar2></something_different></bar2></foo><foo name="2"> <bar1></something></bar1> <bar2></something_different></bar2></foo>...So I use <xsl:for-each select="foo"> to loop through all the <foo>s, output <foo> and then output <bar1> and <bar2> using <xsl:copy-of select="foo/bar"/>.The problem with this - naturally - is that I want only the corresponding <bar*> tags, not all of the document.Is there a way to only output the <bar*>s of the currently looped <foo>, eg, using the name attribute of <foo>?

Link to comment
Share on other sites

So what I get is that the current XSLT is pretty much something like:

<xsl:for-each select="foo"><foo name="{position(current())}"><xsl:copy-of select="./*"></foo></xsl:for-each>

Or what? I mean, it's pretty useless not to use the star when all elements are different. If that doesn't work, then perhaps changing the path to only "." or possibly "descendant::current()"?By the way, you gotta be kidding me for the way your XML looks. The contents of the bars can't begin with a closing tag :) .[edit] I don't think you got my idea. In bar1 you have </something> which technically speaking is a closing tag to the opening tag <something>. If you meant an empty element, it would have been <something/> :) [/edit]

Edited by boen_robot
Link to comment
Share on other sites

By the way, you gotta be kidding me for the way your XML looks. The contents of the bars can't begin with a closing tag :) .
The contents of the <bar>s are supposed to be child tags in <bar> But I should get some sleep, after trying again, I think I don't need the attributes at all... Thanks anyway ^^
Link to comment
Share on other sites

Ok, what I really need to know is this:Let's say I have a tag <foo name="SomeSpecialValue">...and I want the output to have a tag like <bar name="SomeSpecialValue">...How can I get that? I can output <bar name="Constant"> but I want the name to be the output of <xsl:value-of select="@name"/>Is that possibe?

Link to comment
Share on other sites

If the name of the attribute (in this case "name") is known and consistent (doesn't change of some criteria and always exists), then you could use a variable and pass it's value to the attribute. Like this:

<xsl:variable name="name" select="PathToTheSpecialValue" /><bar name="{$name}">

If it's varying and it's not known if it exist, you must use the method I showed you in the other topic (the one including xsl:attribute in it).[edit] Oh, I forgot. You can actually open the variable and have all kinds of things in it. Then it's whole output would be passes instead of the single XPAth expression. Something like this:

<xsl:variable name="name">Some weird stuff you'll want to do</xsl:variable><bar name="{$name}">

[/edit]

Edited by boen_robot
Link to comment
Share on other sites

Just the {$name} thing was what I needed, thanks!I think I have what I need now. But I'm curious: the output doesn't have a .dtd specified, is there a way to add one using the xsl sheet?

Yup. Check all the features of the output element.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...