lithium Posted May 15, 2006 Share Posted May 15, 2006 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 More sharing options...
boen_robot Posted May 15, 2006 Share Posted May 15, 2006 (edited) 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 May 15, 2006 by boen_robot Link to comment Share on other sites More sharing options...
lithium Posted May 15, 2006 Author Share Posted May 15, 2006 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 More sharing options...
lithium Posted May 15, 2006 Author Share Posted May 15, 2006 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 More sharing options...
boen_robot Posted May 15, 2006 Share Posted May 15, 2006 (edited) 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 May 15, 2006 by boen_robot Link to comment Share on other sites More sharing options...
lithium Posted May 15, 2006 Author Share Posted May 15, 2006 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? Link to comment Share on other sites More sharing options...
boen_robot Posted May 15, 2006 Share Posted May 15, 2006 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 More sharing options...
lithium Posted May 15, 2006 Author Share Posted May 15, 2006 Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now