Loop on all var in xslt
I have some issue with Xslt syntax, my stylesheet :
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://saxon.sf.net/">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:for-each select="for $x in(collection('file:///Users/admin/Documents/xmlSoapUi/?select=*.xml;recurse=yes'))return saxon:discard-document($x)//testsuite">
<ident>
<xsl:value-of select="base-uri()"/>
<xsl:if test="matches(base-uri(),'Catalog')">
<xsl:call-template name="summaryCatalog"/>
</xsl:if>
<xsl:if test="matches(base-uri(),'Status')">
<xsl:call-template name="summaryStatus"/>
</xsl:if>
<xsl:if test="matches(base-uri(),'Alarm')">
<xsl:call-template name="summaryAlarm"/>
</xsl:if>
</ident>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template name="summaryCatalog">
<xsl:variable name="avgAddFile" select="avg(testcase[@name='addFile']/@time)"/>
<xsl:variable name="avgGetFile" select="avg(testcase[@name='getFile']/@time)"/>
<xsl:variable name="avgRemoveFile" select="avg(testcase[@name='removeFile']/@time)"/>
<xsl:variable name="avgAddGroup" select="avg(testcase[@name='addGroup']/@time)"/>
<xsl:variable name="avgGetGroup" select="avg(testcase[@name='getGroup']/@time)"/>
<xsl:variable name="avgRemoveGroup" select="avg(testcase[@name='removeGroup']/@time)"/>
<xsl:variable name="avgAddMedia" select="avg(testcase[@name='addMedia']/@time)"/>
<xsl:variable name="avgGetMedia" select="avg(testcase[@name='getMedia']/@time)"/>
<xsl:variable name="avgRemoveMedia" select="avg(testcase[@name='removeMedia']/@time)"/>
<xsl:variable name="avgAddMediaComponent" select="avg(testcase[@name='addMediaComponent']/@time)"/>
<xsl:variable name="avgGetMediaComponent" select="avg(testcase[@name='getMediaComponent']/@time)"/>
<xsl:variable name="avgRemoveMediaComponent" select="avg(testcase[@name='removeMediaComponent']/@time)"/>
<xsl:variable name="avgAddOpus" select="avg(testcase[@name='addOpus']/@time)"/>
<xsl:variable nam开发者_如何转开发e="avgGetOpus" select="avg(testcase[@name='getOpus']/@time)"/>
<xsl:variable name="avgRemoveOpus" select="avg(testcase[@name='removeOpus']/@time)"/>
<xsl:variable name="avgAddTrack" select="avg(testcase[@name='addTrack']/@time)"/>
<xsl:variable name="avgGetTrack" select="avg(testcase[@name='getTrack']/@time)"/>
<xsl:variable name="avgRemoveTrack" select="avg(testcase[@name='removeTrack']/@time)"/>
<testChargeCatalog>
<avgAddFile><xsl:value-of select="format-number($avgAddFile,'0.00000')"/></avgAddFile>
<avgGetFile><xsl:value-of select="format-number($avgGetFile,'0.00000')"/></avgGetFile>
<avgRemoveFile><xsl:value-of select="format-number($avgRemoveFile,'0.00000')"/> </avgRemoveFile>
<avgAddGroup><xsl:value-of select="format-number($avgAddGroup,'0.00000')"/></avgAddGroup>
<avgGetGroup><xsl:value-of select="format-number($avgAddGroup,'0.00000')"/></avgGetGroup>
<avgRemoveGroup><xsl:value-of select="format-number($avgAddGroup,'0.00000')"/></avgRemoveGroup>
<avgAddMedia><xsl:value-of select="format-number($avgAddMedia,'0.00000')"/></avgAddMedia>
<avgGetMedia><xsl:value-of select="format-number($avgAddMedia,'0.00000')"/></avgGetMedia>
<avgRemoveMedia><xsl:value-of select="format-number($avgAddMedia,'0.00000')"/></avgRemoveMedia>
<avgAddMediaComponent><xsl:value-of select="format-number($avgAddMediaComponent,'0.00000')"/></avgAddMediaComponent>
<avgGetMediaComponent><xsl:value-of select="format-number($avgGetMediaComponent,'0.00000')"/></avgGetMediaComponent>
<avgRemoveMediaComponent><xsl:value-of select="format-number($avgRemoveMediaComponent,'0.00000')"/></avgRemoveMediaComponent>
<avgAddOpus><xsl:value-of select="format-number($avgAddOpus,'0.00000')"/></avgAddOpus>
<avgGetOpus><xsl:value-of select="format-number($avgGetOpus,'0.00000')"/></avgGetOpus>
<avgRemoveOpus><xsl:value-of select="format-number($avgRemoveOpus,'0.00000')"/></avgRemoveOpus>
<avgAddTrack><xsl:value-of select="format-number($avgAddTrack,'0.00000')"/></avgAddTrack>
<avgGetTrack><xsl:value-of select="format-number($avgGetTrack,'0.00000')"/></avgGetTrack>
<avgRemoveTrack><xsl:value-of select="format-number($avgRemoveTrack,'0.00000')"/></avgRemoveTrack>
</testChargeCatalog>
</xsl:template>
<xsl:template name="summaryStatus">
<xsl:variable name="avgAddStatus" select="avg(testcase[@name='addStatus']/@time)"/>
<xsl:variable name="avgGetStatus" select="avg(testcase[@name='getStatus']/@time)"/>
<testChargeStatus>
<avgAddStatus><xsl:value-of select="format-number($avgAddStatus,'0.00000')"/></avgAddStatus>
<avgGetStatus><xsl:value-of select="format-number($avgGetStatus,'0.00000')"/></avgGetStatus>
</testChargeStatus>
</xsl:template>
<xsl:template name="summaryAlarm">
<xsl:variable name="avgAddAlarm" select="avg(testcase[@name='addAlarm']/@time)"/>
<xsl:variable name="avgGetAlarm" select="avg(testcase[@name='getAlarm']/@time)"/>
<testChargeAlarm>
<avgAddAlarm><xsl:value-of select="format-number($avgAddAlarm,'0.00000')"/></avgAddAlarm>
<avgGetAlarm><xsl:value-of select="format-number($avgGetAlarm,'0.00000')"/></avgGetAlarm>
</testChargeAlarm>
</xsl:template>
what i want to do is create a template with an <xsl:for-each select="AllVariables">
and then apply some transform to the value of the variables.
I dont know if it is possible, because i haven't seen any exemple online.
Thanks for helping
It's not clear to me, why you can't do:
<testChargeCatalog>
<xsl:for-each-group select="testcase" group-by="@name">
<xsl:element name="avg{upper-case(
substring(
current-grouping-key(),
1,
1
)
)
}{substring(current-grouping-key(),2)}">
<xsl:value-of select="format-number(
avg(current-group()/@time),
'0.00000'
)"/>
</xsl:element>
<xsl:for-each-group>
</testChargeCatalog>
Well you can (with XSLT 2.0 you seem to use) create one variable which is a sequence of several values and then you can use for-each
to process that sequence of values. And of course you can create temporary results of nodes you can process further with for-each or apply-templates. So that is the approach you should choose, instead of declaring ten variable each having one value you either need to create one variable having a sequence of ten primitive values or you need to create a variable holding a temporary tree of XML nodes, then you can use apply-templates or for-each on those nodes in the temporary tree.
You could create an xsl:variable
that is the union of all xsl:variable
s in the xsl:import
/xsl:include
hierarchy starting from a primary stylesheet module.
However, you generally don't get the values, you will only get the expression specified in the select
attribute (or the body of the variable), but you will not be able to "get the values" due to the folowing main reasons;
It is not possible to dynamically evaluate any XPath expression in XSLT/XPath (version 1.0 and 2.0).
Even if dynamic evaluation were possible (say in version 3.0), it is not possible to re-create the dynamic context of an expression -- for example a variable is locally defined and uses other variables or parameters or context-dependent functions, such as
position()
orlast()
.
精彩评论