1 package net.sf.saxon.style; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.ExpressionTool; 4 import net.sf.saxon.instruct.Executable; 5 import net.sf.saxon.instruct.ForEach; 6 import net.sf.saxon.om.AttributeCollection; 7 import net.sf.saxon.om.Axis; 8 import net.sf.saxon.sort.SortExpression; 9 import net.sf.saxon.sort.SortKeyDefinition; 10 import net.sf.saxon.trans.XPathException; 11 import net.sf.saxon.type.ItemType; 12 import net.sf.saxon.value.EmptySequence; 13 14 15 18 19 public class XSLForEach extends StyleElement { 20 21 Expression select = null; 22 23 27 28 public boolean isInstruction() { 29 return true; 30 } 31 32 35 36 protected boolean isPermittedChild(StyleElement child) { 37 return (child instanceof XSLSort); 38 } 39 40 45 46 protected ItemType getReturnedItemType() { 47 return getCommonChildItemType(); 48 } 49 50 54 55 public boolean mayContainSequenceConstructor() { 56 return true; 57 } 58 59 public void prepareAttributes() throws XPathException { 60 61 AttributeCollection atts = getAttributeList(); 62 63 String selectAtt = null; 64 65 for (int a=0; a<atts.getLength(); a++) { 66 int nc = atts.getNameCode(a); 67 String f = getNamePool().getClarkName(nc); 68 if (f==StandardNames.SELECT) { 69 selectAtt = atts.getValue(a); 70 } else { 71 checkUnknownAttribute(nc); 72 } 73 } 74 75 if (selectAtt==null) { 76 reportAbsence("select"); 77 } else { 78 select = makeExpression(selectAtt); 79 } 80 81 } 82 83 public void validate() throws XPathException { 84 checkWithinTemplate(); 85 checkSortComesFirst(false); 86 select = typeCheck("select", select); 87 } 88 89 public Expression compile(Executable exec) throws XPathException { 90 SortKeyDefinition[] sortKeys = makeSortKeys(); 91 Expression sortedSequence = select; 92 if (sortKeys != null) { 93 sortedSequence = new SortExpression(select, sortKeys); 94 ExpressionTool.makeParentReferences(sortedSequence); 95 } 96 97 Expression block = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true); 98 if (block == null) { 99 return EmptySequence.getInstance(); 101 } 102 try { 103 ForEach inst = new ForEach(sortedSequence, block.simplify(getStaticContext())); 104 ExpressionTool.makeParentReferences(inst); 105 return inst; 106 } catch (XPathException err) { 107 compileError(err); 108 return null; 109 } 110 } 111 112 113 } 114 115 | Popular Tags |