XSLT Sort by second element if first element equals zero

时间:2016-08-31 18:18:20

标签: xml sorting xslt

I'm trying to sort some statistical information for use in InDesign and would like to do a conditional sort which uses a second element if the first has a value of zero. Essentially, I would like to sort the information by "shots/@sh" if "shots/@g" has a value of zero. Source XML below. Thank you.

   <player uni="9" code="9" name="" checkname="" class="" gp="1">
  <shots g="0" a="0" sh="1" sog="1" ps="0" psatt="0"></shots>
  <goaltype gw="0" ua="0" fg="0" ot="0" en="0" hat="0" gt="0" so="0"></goaltype>
  <penalty count="0" red="0" yellow="0" green="0" fouls="0"></penalty>
  <misc minutes="0" dsave="0"></misc>
</player>
<player uni="10" code="10" name="Ali Al-Gashamy" checkname="AL-GASHAMY,ALI" class="" gp="1" gs="1" pos="f">
  <shots g="0" a="0" sh="2" sog="0" ps="0" psatt="0"></shots>
  <goaltype gw="0" ua="0" fg="0" ot="0" en="0" hat="0" gt="0" so="0"></goaltype>
  <penalty count="0" red="0" yellow="1" green="0" fouls="0"></penalty>
  <misc minutes="82" dsave="0"></misc>
</player>
<player uni="11" code="11" name="Severin Soerlie" checkname="SOERLIE,SEVERIN" class="" gp="1" gs="1" pos="mf">
  <shots g="0" a="0" sh="3" sog="0" ps="0" psatt="0"></shots>
  <goaltype gw="0" ua="0" fg="0" ot="0" en="0" hat="0" gt="0" so="0"></goaltype>
  <penalty count="0" red="0" yellow="0" green="0" fouls="0"></penalty>
  <misc minutes="90" dsave="0"></misc>
</player>

1 个答案:

答案 0 :(得分:0)

would like to do a conditional sort which uses a second element if the first has a value of zero.

How about:

<xsl:sort select="shots[@g!=0]/@g | shots[@g=0]/@sh" data-type="number" order="ascending"/>

Added:

I basically just want to sort by goals first, but if we don't score I'd like it to sort by shots instead.

That's a completely different requirement. For this, you should use two sort instructions:

<xsl:sort select="shots/@g" data-type="number" order="ascending"/>
<xsl:sort select="shots/@sh" data-type="number" order="ascending"/>

assuming you don't care if players with the same number of goals are sorted internally by shots, even if the number of goals is not zero. Otherwise use:

<xsl:sort select="shots[@g=0]/@sh" data-type="number" order="ascending"/>

as the second sort instruction.