xml - xslt condition output one by one -
hope has suggestion this:
i need have in each 'a', 'b' have @n equal or bigger @n of 'a' in are.
i using xslt 2.0 , saxon-he 9.6.0.5
xml source:
<blabla> <a n="2"></a> <a n="6"></a> <b n="6"></b> <b n="1"></b> <b n="4"></b> </blabla>
xslt:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" exclude-result-prefixes="xs" version="2.0"> <xsl:template match="blabla"> <all> <xsl:for-each select="//a"> <a> <xsl:attribute name="n" select="./@n"/> <xsl:for-each select="//b"> <xsl:if test="./@n[. >= //a/@n]"> <b> <xsl:attribute name="n" select="./@n"/> </b> </xsl:if> </xsl:for-each> </a> </xsl:for-each> </all> </xsl:template>
what have is:
<all> <a n="2"> <b n="6"/> <b n="4"/> </a> <a n="6"> <b n="6"/> </a> </all>
what have instead is:
<all> <a n="2"> <b n="6"/> <b n="4"/> </a> <a n="6"> <b n="6"/> <b n="4"/> </a> </all>
i not sure if whole approach wrong or if have adjust something.
just completeness, function trying same thing. output nothing when create elements inside function:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:foo="http://whatever"> <xsl:function name="foo:test"> <xsl:param name="a"/> <xsl:param name="b"/> <xsl:for-each select="$a"> <a> <xsl:attribute name="n"> <xsl:value-of select="$a"/> </xsl:attribute> <xsl:if test="$b >= $a"> <b> <xsl:attribute name="n"> <xsl:value-of select="$b"/> </xsl:attribute> </b> </xsl:if> </a> </xsl:for-each> </xsl:function> <xsl:template match="/"> test 1: <xsl:value-of select="foo:test(//a/@n, //b/@n)"/> test 2: <xsl:value-of select="foo:test(7, 6)"/> test 3: <xsl:value-of select="foo:test(3, 6)"/> </xsl:template>
the same function, without creating elements, works fine numbers, not if put parameters xpath expression match source document (it outputs everything).
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:foo="http://whatever"> <xsl:function name="foo:foo:test"> <xsl:param name="a"/> <xsl:param name="b"/> <xsl:if test="$b >= $a"> a: <xsl:value-of select="$a"/> b: <xsl:value-of select="$b"/> </xsl:if> </xsl:function> <xsl:template match="/"> test 1: <xsl:value-of select="foo:test(//a/@n, //b/@n)"/> test 2: <xsl:value-of select="foo:test(7, 6)"/> test 3: <xsl:value-of select="foo:test(3, 6)"/> </xsl:template> </xsl:stylesheet>
output:
test 1: a: 2 6 b: 6 1 4 test 2: test 3: a: 3 b: 6
i don't need function; if have suggestions doing without function, fine. thank you!
try:
<xsl:template match="blabla"> <all> <xsl:for-each select="a"> <a n="{@n}"> <xsl:copy-of select="../b[@n >= current()/@n]"/> </a> </xsl:for-each> </all> </xsl:template>
Comments
Post a Comment