XSLT - Problem with foreach and incremented attribute name
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 s开发者_如何学运维uggestions? Thanks!
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).
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>
精彩评论