问题
With help of Tim from my previous post, i was able to get the Muenchian grouping working for my test xml/xsl. But while working with my actual xml i encountered a different scenario as below. The input xml looks like, reduced it for simplicity. As you see the <name>Status</name> <value>Existing</value>
there are many other entries in actual xml.
<message>
<requisition>
<data-values>
<data-value multi-valued="false">
<name>Test_Grid-1.Name</name> <value>1</value>
</data-value>
<data-value multi-valued="false">
<name>Test_Grid-1.SupportType</name> <value>Monthly,Quarterly</value>
</data-value>
<data-value multi-valued="false">
<name>Test_Grid-2.Name</name> <value>2</value>
</data-value>
<data-value multi-valued="false">
<name>Test_Grid-2.SupportType</name> <value>Monthly</value>
</data-value>
<data-value multi-valued="false">
<name>Status</name> <value>Existing</value>
</data-value>
</data-values>
</requisition>
<agent-parameter multi-valued="false">
<name>ActionType</name> <value>New</value>
</agent-parameter>
<agent-parameter multi-valued="false">
<name>Dictionary</name> <value>Test_Grid</value>
</agent-parameter>
<agent-parameter multi-valued="false">
<name>ActionName</name> <value>SupportData</value>
</agent-parameter>
</message>
The XSL i am using is:
<xsl:key name="record" match="data-value" use="substring-before(name, '.')" />
<xsl:template match="message">
<record>
<xsl:apply-templates select="//data-value[generate-id() = generate-id(key('record', substring-before(name, '.'))[1])]" mode="record" />
</record>
</xsl:template>
<xsl:template match="data-value" mode="record">
<rowData>
<name><xsl:value-of select="$ServiceItemName" /></name>
<xsl:apply-templates select="key('record', substring-before(name, '.'))" />
</rowData>
</xsl:template>
<xsl:template match="data-value">
<xsl:if test="starts-with(name,concat($Dictionary,'-'))">
<rowAttribute name="{substring-after(name, '.')}"><xsl:value-of select="value" /> </rowAttribute>
</xsl:if>
</xsl:template>
The actual input xml contains different data-values (other than Test_Grid-1 or Test_Grid-2 etc). In that scenario the xsl is pulling all those elements and showing inside . Is there any way i can filter the values only starting with "Test_Grid-" (i have the agent-parameter with name Dictionary
and value as "Test_Grid") and the xsl will pull only those data-values where the name starts-with concat($Dictionary,'-')
. I tried something like below
<xsl:template match="data-value">
<xsl:if test="starts-with(name,concat($Dictionary,'-'))">
<rowAttribute name="{substring-after(name, '.')}"><xsl:value-of select="value" /></rowAttribute>
</xsl:if>
But its showing empty elements in output like below along with the required output.
<record>
<name>SupportData</ext:name>
</record>
<record>
<name>SupportData</name>
<rowData>
<rowAttribute name="Name">Record1</rowAttribute>
<rowAttribute name="SupportType">Quarterly</rowAttribute>
</rowData>
</record>
Is there a way i can show the element only when it has some rowData elements in it.
Any and all help will be really appreciated.
回答1:
To ignore data-value elements whose name do not start with 'Test_Grid-' you can use the xpath function 'starts-with'. You can add a condition to the xpath expression to filter the data-value elements
data-value[starts-with(name, 'Test_Grid-')]
You can actually add this to the xsl:key match on data-value, or to the xsl:apply-templates where you select them. For example, you could either do this:
<xsl:key name="record" match="data-value[starts-with(name, 'Test_Grid-')]" use="substring-before(name, '.')" />
Or alternatively, you could do this
<xsl:apply-templates
select="requisition/data-values/data-value
[starts-with(name, 'Test_Grid-')]
[generate-id() = generate-id(key('record', substring-before(name, '.'))[1])]"
mode="record" />
It may be more efficient to put it as part of the key. By putting it as part of the key, the check for distinct elements will only consider ones beginning with 'Test-Grid-'.
来源:https://stackoverflow.com/questions/18942147/muenchian-grouping-display-second-level-base-on-row-count