示例#1
0
void mxslt_transform_value_of(xsltTransformContextPtr ctxt, xmlNodePtr node, xmlNodePtr inst, xsltElemPreCompPtr comp) {
  mxslt_doc_t * document=mxslt_get_state()->document;
  char * var, * value;

  mxslt_doc_debug_print(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE0, 
		    "%s: <modxslt:value-of called\n", mxslt_debug_string(document->localfile));

    /* Check input parameters */
  if(ctxt == NULL || node == NULL || inst == NULL)
    return; 

    /* Check type is correct */
  if(inst->type != XML_ELEMENT_NODE)
    return;

    /* Verify we have some attribute */
  if(inst->properties == NULL || inst->properties->type != XML_ATTRIBUTE_NODE ||
     !inst->properties->children || inst->properties->children->type != XML_TEXT_NODE ||
     !inst->properties->children->content) {
    xsltTransformError(ctxt, ctxt->style, inst, "<modxslt:value-of select='... no valid attribute specifyed!");
    return;
  }

    /* Get attribute value */
  if(!xmlStrEqual(inst->properties->name, (xmlChar *)"select")) {
    xsltTransformError(ctxt, ctxt->style, inst, "<modxslt:value-of %s='... unknown attribute!", inst->properties->name);
    return;
  }

    /* Warn if we were provided more
     * attributes than expected */
  if(inst->properties->next) 
    xsltTransformError(ctxt, ctxt->style, inst, "<modxslt:value-of %s='... unknown additional attribute!", 
		       inst->properties->next->name);

  var=(char *)inst->properties->children->content;

    /* Now, let's do it... */
  value=mxslt_yy_str_parse(document, var, strlen(var));
  if(!value)
    value="";

  mxslt_doc_debug_print(document, MXSLT_DBG_LIBXML, "setting variable '%s' to value '%s'\n", 
		    mxslt_debug_string(var), value);

    /* XXX: uhm... how do I know I always need escaping? what 
     * if escaping was disabled? */
  var=(char *)xsltCopyTextString(ctxt, ctxt->insert, (xmlChar *)value, 0); 
  xfree(value);

  if(!var)
    xsltTransformError(ctxt, ctxt->style, inst, "<modxslt:value-of %s='...xsltCopyTextString failed!", 
		       inst->properties->next->name);

  return;
}
示例#2
0
/**
 * xsltNumberFormat:
 * @ctxt: the XSLT transformation context
 * @data: the formatting informations
 * @node: the data to format
 *
 * Convert one number.
 */
void
xsltNumberFormat(xsltTransformContextPtr ctxt,
		 xsltNumberDataPtr data,
		 xmlNodePtr node)
{
    xmlBufferPtr output = NULL;
    int amount, i;
    double number;
    xsltFormat tokens;

    if (data->format != NULL) {
        xsltNumberFormatTokenize(data->format, &tokens);
    }
    else {
        xmlChar *format;

	/* The format needs to be recomputed each time */
        if (data->has_format == 0)
            return;
	format = xsltEvalAttrValueTemplate(ctxt, data->node,
					     (const xmlChar *) "format",
					     XSLT_NAMESPACE);
        if (format == NULL)
            return;
        xsltNumberFormatTokenize(format, &tokens);
	xmlFree(format);
    }

    output = xmlBufferCreate();
    if (output == NULL)
	goto XSLT_NUMBER_FORMAT_END;

    /*
     * Evaluate the XPath expression to find the value(s)
     */
    if (data->value) {
	amount = xsltNumberFormatGetValue(ctxt->xpathCtxt,
					  node,
					  data->value,
					  &number);
	if (amount == 1) {
	    xsltNumberFormatInsertNumbers(data,
					  &number,
					  1,
					  &tokens,
					  output);
	}

    } else if (data->level) {

	if (xmlStrEqual(data->level, (const xmlChar *) "single")) {
	    amount = xsltNumberFormatGetMultipleLevel(ctxt,
						      node,
						      data->countPat,
						      data->fromPat,
						      &number,
						      1);
	    if (amount == 1) {
		xsltNumberFormatInsertNumbers(data,
					      &number,
					      1,
					      &tokens,
					      output);
	    }
	} else if (xmlStrEqual(data->level, (const xmlChar *) "multiple")) {
	    double numarray[1024];
	    int max = sizeof(numarray)/sizeof(numarray[0]);
	    amount = xsltNumberFormatGetMultipleLevel(ctxt,
						      node,
						      data->countPat,
						      data->fromPat,
						      numarray,
						      max);
	    if (amount > 0) {
		xsltNumberFormatInsertNumbers(data,
					      numarray,
					      amount,
					      &tokens,
					      output);
	    }
	} else if (xmlStrEqual(data->level, (const xmlChar *) "any")) {
	    amount = xsltNumberFormatGetAnyLevel(ctxt,
						 node,
						 data->countPat,
						 data->fromPat,
						 &number);
	    if (amount > 0) {
		xsltNumberFormatInsertNumbers(data,
					      &number,
					      1,
					      &tokens,
					      output);
	    }
	}
    }
    /* Insert number as text node */
    xsltCopyTextString(ctxt, ctxt->insert, xmlBufferContent(output), 0);

    xmlBufferFree(output);

XSLT_NUMBER_FORMAT_END:
    if (tokens.start != NULL)
	xmlFree(tokens.start);
    if (tokens.end != NULL)
	xmlFree(tokens.end);
    for (i = 0;i < tokens.nTokens;i++) {
	if (tokens.tokens[i].separator != NULL)
	    xmlFree(tokens.tokens[i].separator);
    }
}