tags:

views:

35

answers:

2

I'm using XSLT and would like to transform this:

<attr>
    <header name="UpdateInformation1">
        <detail name="info">blah</detail>
    </header> 
    <header name="UpdateInformation2">
        <detail name="info">blah2</detail>
    </header> 
...other headers with different names...
</attr>

To this:

<UpdateInformation>
   <info>blah</info>
</UpdateInformation>
<UpdateInformation>
   <info>blah2</info>
</UpdateInformation>
...

I've been trying to do this using a foreach, but I'm not having much success. Heres what I currently have, but wildcards don't work in this type of context:

* WRONG *

<xsl:for-each select="attr/header[@name='UpdateInformation*']">
    <UpdateInformation>
    <Info>
          <xsl:value-of select="detail[@name='info']"/>
        </info>
    </UpdateInformation>
</xsl:for-each>

* WRONG *

Any suggestions? Thanks!

+2  A: 

Doing this with the xsl:for-each element:

<xsl:for-each select="header[starts-with(@name, 'UpdateInformation')]">
 <UpdateInformation>
  <Info>
   <xsl:value-of select="detail"/>
  </info>
 </UpdateInformation>
</xsl:for-each>

Using a xsl:template would be a better way to do this in xslt, as this is the strength of it:

<xsl:template match="header[starts-with(@name, 'UpdateInformation')]">
 <UpdateInformation>
  <Info>
   <xsl:value-of select="detail"/>
  </info>
 </UpdateInformation>
</xsl:template>
Oded
Sorry, changed my question a bit...There would be more headers of different names. Not just UpdateInformations, and I only want to select the UpdateInformations
mawaldne
Updated answer following update to question
Oded
+1 for "use a template" :-)
Tomalak
+4  A: 

Use something like this:

<xsl:for-each select="attr/header[starts-with(@name, 'UpdateInformation')]">
    <UpdateInformation>
        <Info>
            <xsl:value-of select="detail[@name='info']"/>
        </info>
    </UpdateInformation>
</xsl:for-each>

EDITED: Corrected XPath expression per comments (below).

Drew Wills
I actually used <xsl:for-each select="attr/header[starts-with(@name, 'UpdateInformation')]"> and that works. Thanks Drew
mawaldne
This won't work, as none of the `/detail` nodes has a `@name` attribute that starts with `UpdateInformation`
Oded
Ack -- good catch.Questions here get answered so quickly... I'm usually scrambling to write a response before 7 others say what I have to say and everyone has moved on. =P
Drew Wills