Change flow of sequence of policies using Java Script in apigee? - apigee

If i want XSLTransform-1 policy to get executed only when the URL has "tennis" keyword in it,how can i do it using Java Script policy in apigee?
URL: http://shaleen-test.apigee.net/v1/espn--api/tennis/athletes/296? apikey=rgnmd3naaw2qwv79fdtjgz77
Policy:XSLTransform-1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XSL async="false" continueOnError="false" enabled="true" name="xsltransform-1">
<DisplayName>XSLTransform-1</DisplayName>
<FaultRules/>
<Properties/>
<Source>response</Source>
<ResourceURL>xsl://xsltransform-1</ResourceURL>
<Parameters ignoreUnresolvedVariables="true"/>
<OutputVariable>abc</OutputVariable>
</XSL>
XSL File:xsltransform-1
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<xsl:template match="/">
<html>
<body>
<h1>PROFILE OF PLAYERS</h1>
<h2> <xsl:value-of select="Root/sports/name" /> </h2>
<h3>Report Details</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>PLAYER ID</th>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>FULL NAME</th>
<th>SHORT NAME</th>
</tr>
<xsl:for-each select="Root/sports/leagues/athletes">
<tr>
<td><xsl:value-of select="id" /></td>
<td><xsl:value-of select="firstName" /></td>
<td><xsl:value-of select="lastName" /></td>
<td><xsl:value-of select="fullName" /></td>
<td><xsl:value-of select="shortName" /></td>
<!--<td><xsl:value-of select="Root/sports/leagues/athletes/id" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/firstName" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/lastName" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/fullName" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/shortName" /></td>-->
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

This can be achieved by have a conditional execution of the XSLT.You can use extract variables to extract a particular value from the request path.And if that variable's value is 'tennis' you can execute the XSLT Policy.
Is there any specific reason why you want to control the flow from Javascript?

Related

how to redirect to particular method in controller in spring mvc, xml based configuration?

how to redirect to particular method in controller in spring mvc, xml based configuration?
I am submitting data from JSP, and want to go to particular method in controller.
I am using XML based config without MAVEN.
Thanks
Create a JSP file that will display Person from to our users.
File: /WebContent/WEB-INF/jsp/person.jsp
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<body>
<form:form method="post" action="addPerson.html">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Person"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
Then you need to create Model class for Person.java
public class Person {
private String firstname;
private String lastname;
//.. getter and setter for all above fields.
}
Create PersonController.java
public class PersonController {
#RequestMapping(value = "/addPerson", method = RequestMethod.POST)
public String addContact(#ModelAttribute("person")
Person person, BindingResult result) {
System.out.println("First Name:" + person.getFirstname() +
"Last Name:" + person.getLastname());
return "redirect:person.html";
}
}
For ViewResolver create spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
-<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<context:component-scan base-package="net.viralpatel.spring3.controller"/>
-<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="viewResolver">
<property value="org.springframework.web.servlet.view.JstlView" name="viewClass"/>
<property value="/WEB-INF/jsp/" name="prefix"/>
<property value=".jsp" name="suffix"/>
</bean>
</beans>
You can use MultiActionController. Let's say this is your .jsp file:
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<body>
<form:form method="post" action="/yourapp/addPerson">
<table>
<tr>
<td><form:label path="firstname">First Name</form:label></td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Person"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
and this is your model class:
public class Person {
private String firstname;
private String lastname;
//.. getter and setter for all above fields.
}
Your controller would look like this:
public class PersonController extends MultiActionController{
public ModelAndView addPerson(HttpServletRequest request, HttpServletResponse response) throws Exception {
//... your code here
}
public ModelAndView listPerson(HttpServletRequest request, HttpServletResponse response) throws Exception {
//..your code here
}
}
and your .XML file would look like this.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- for mapping controllers -->
<bean name="/addPerson" class="yourpackage.PersonController" />
<bean name="/listPerson" class="yourpackage.PersonController" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Here's an example. http://www.mkyong.com/spring-mvc/spring-mvc-multiactioncontroller-example/

QWebException: "'NoneType' object is not callable" while evaluating 'has_shortage_so_lines(data)==True'

I found the another question at here. But there is no answer.
I got the error
QWebException: "'NoneType' object is not callable" while evaluating 'has_shortage_so_lines(data)==True'
This is my report_view.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- External Consumption Report -->
<!-- Landscape Paper Format-->
<record id="paperformat_LetterLandscape" model="report.paperformat">
<field name="name">US Letter Landscape</field>
<field name="default" eval="True"/>
<field name="format">Letter</field>
<field name="page_height">0</field>
<field name="page_width">0</field>
<field name="orientation">Landscape</field>
<field name="margin_top">15</field>
<field name="margin_bottom">15</field>
<field name="margin_left">2</field>
<field name="margin_right">2</field>
<field name="header_line" eval="False"/>
<field name="header_spacing">7</field>
<field name="dpi">100</field>
</record>
<!--Report XLS-->
<report
id="action_report_shortage_xls"
model="sale.order"
string="Shortage Report"
report_type="qweb-html"
name="sale.report_shortage_xls"
file="sale.report_shortage_xls"/>
<!--Paper Format to shortage report-->
<record id="action_report_shortage_xls" model="ir.actions.report.xml">
<field name="paperformat_id" ref="stock.paperformat_LetterLandscape"/>
</record>
</data>
</openerp>
This is my report_xls.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Translatable template -->
<template id="report_shortage_xls">
<div class="workbook">
<div class="worksheet" name="Shortage Report">
<table>
<tbody>
<tr>
<td easyfx="font: height 200;align: horizontal left,vert center">Printed On :</td>
<td><span t-usertime="%d-%m-%Y %H:%M" /></td>
</tr>
<tr>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="17" easyfx="font: height 400, bold on;align: horizontal center,vert center">Shortage Report</th>
</tr>
<!--<tr>
<th>
<span t-esc="has_shortage_so_lines(o.id)"/>
</th>
</tr>-->
</thead>
</table>
<t t-if="has_shortage_so_lines(data)==True">
<table>
<thead>
<tr>
<th easyfx="font:height 200, bold on;align: horizontal center,vert center;">Date</th>
<th easyfx="font:height 200, bold on;align: horizontal center,vert center;">SO No</th>
<th easyfx="font:height 200, bold on;align: horizontal center,vert center;">Customer Ref No</th>
</tr>
</thead>
</table>
</t>
</div>
</div>
</template>
<!-- Main template -->
<template id="xls_report_shortage">
<t t-call="report.html_container">
<t t-foreach="doc_ids" t-as="doc_id">
<t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', 'sale.report_shortage_xls')"/>
</t>
</t>
</template>
</data>
</openerp>
This is my report.py
class ShortageReport(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
super(ShortageReport, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
'get_date_from': self.get_date_from,
'get_date_to': self.get_date_to,
'has_shortage_so_lines': self.has_shortage_so_lines,
'get_shortage_lines': self.get_shortage_lines,
})
def has_shortage_so_lines(self, order_id):
_logger.info("sale order id >>> : %r", order_id)
lines_obj = self.get_shortage_so_lines(order_id)
if lines_obj:
return True
return False
class report_shortage_xls(osv.AbstractModel):#(models.AbstractModel):
_name = 'report.stock.report_shortage_xls'
_inherit = 'report.abstract_report'
_template = 'stock.report_shortage_xls'
_wrapped_report_class = ShortageReport
Is there any way to export the xls from odoo. Thank you so much for your helping.
Make sure here
_template = 'stock.report_shortage_xls'
it must be module name with report name, you have created report for sale.order and you have given stock module name, is it correct in your case ?
For more information you should refer our blog on Qweb Report.

Generating XSLT for the given XML. Error: Mismatch

I Have an XML something like this
<NavigatorItems>
<Navigator Name="Product">
<ModifierName>Product1</ModifierName>
<ModifierLink>www.Product1.com</ModifierLink>
<ModifierName>Product2</ModifierName>
<ModifierLink>www.Product2.com</ModifierLink>
<ShowAll>www.ProductMain.com</ShowAll>
</Navigator>
<Navigator Name="Article">
<ModifierName>Article1</ModifierName>
<ModifierLink>www.Article1.com</ModifierLink>
<ModifierName>Article2</ModifierName>
<ModifierLink>www.Article2.com</ModifierLink>
<ShowAll>www.ArticleMain.com</ShowAll>
</Navigator>
</NavigatorItems>
I Need to show something like this:
I tried the following XSLT but it throws some error (mismatch):
XML Parsing Error: mismatched tag. Expected: </ModifierName>
My Code:
<xsl:for-each select="NavigatorItems/Navigator">
<xsl:variable name="link" select="ModifierLink"/>
<tr>
<td><a href ="{$link}"><xsl:value-of select="ModifierName"/></td>
</tr>
<xsl:test select="ShowAll">
<xsl:variable name="linkShowAll" select="ShowAll"/>
<tr> <td> <a href="{$linkShowAll}"> View More Results <td> </tr>
</xsl:test>
</xsl:for-each>
Where Am I going wrong ? Please Suggest...
There were a number of problems with your code. I think I've fixed them all, but let me know if you have any problems with this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:for-each select="NavigatorItems/Navigator">
<xsl:variable name="link" select="ModifierLink"/>
<tr>
<td>
<a><xsl:attribute name="href"><xsl:value-of select="ModifierLink"/></xsl:attribute><xsl:value-of select="ModifierName" /></a>
</td>
</tr>
<xsl:if test select="ShowAll != ''">
<tr>
<td>
<a><xsl:attribute name="href"><xsl:value-of select="ShowAll"/>View More Results</a>
</td>
</tr>
</xsl:test>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

limit number of characters in a string - XSLT

I am using a third party asp.net control to pull and display the latest content from the database. The control pulls the title of the latest published content using a xsl file. My issue is the title(content piece) being too long. They place I used to display has no room for about 100 characters. I need to trim(not the white spaces) end part and may be limit it to a few words or some characters. It uses the xsl file -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<xsl:for-each select="Collection/Content">
<tr>
<td>
<a>
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="Type ='Assets' or Type = 8 ">
javascript:void window.open('showcontent.aspx?id=<xsl:value-of select="ID"/>')
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="QuickLink"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="Title"/>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
This part, <xsl:value-of select="Title"/> is where you the title is and I need to shorten it.. (putting ... in the end perhaps)
How can I do it? Can I get this done in the xsl file itself without using JQuery? Many thanks,
<xsl:choose>
<xsl:when test="string-length(Title) > 100">
<xsl:value-of select="substring(Title, 1, 97)" />...
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Title" />
</xsl:otherwise>
</xsl:choose>
You can use the substring(text, startingIndex, length) XSLT function:
<xsl:value-of select="substring(Title, 1, 100)"/>
Note that substring index starts from 1, instead of the usual 0.
More at http://zvon.org/xxl/XSLTreference/Output/xpathFunctionIndex.html
Note also, that .NET does not implements XSLT 2.0, only XSLT 1.0 (hence the above reference: its the list of XSLT 1.0 functions).
Limit the 20 characters
If your code likes this
<xsl:value-of select="EmpBio" disable-output-escaping="yes"/>
Just change with new one written below
<xsl:value-of select="substring(BioData, 1, 20)" disable-output-escaping="yes"/>
Note that the quotation marks are removed from the element name BioData

How to reverse date format in XSLT?

I’m working on a site In that site some pages gets data from XML through XSLT. But the date is displayed as YYYY-MM-DD which ideally is taken from the XML which was in this format. I would like to convert this format to DD-MM-YYYY through XSLT or some other possible way.
Please suggest me an idea to go ahead or provide me the code to achieve this ASAP.
This is the format of xml giving
<published date="2009-09-28T07:06:00 CET" />
and i want to convert this into
<published date="28-09-2009T07:06:00 CET" />
and this is xsl file
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table class="bdr-bot" width="100%" border="0" cellspacing="0" cellpadding="0" style="clear:both">
<tr>
<th width="15%" class="bdr">Date</th>
<th class="bdr">Title</th>
</tr>
<xsl:for-each select="hexML/body/press_releases/press_release">
<xsl:if test="contains(published/#date, '2009')">
<tr>
<td valign="top">
<xsl:value-of select="substring-before(published/#date, 'T')"/>
</td>
<td valign="top">
<xsl:value-of select="headline"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Now tell me the solution? is this possible with fn:reverse?
If the XML is in the format YYYY-MM-DD, you should be able to use Xpath's tokenize function to split up your string where - occurs, and then reorder it. Something akin to:
<xsl:variable name="dt" value="tokenize(Date, '-')"/>
<xsl:value-of select="concat(dt[3],'-',dt[2],'-',dt[1])"/>
This is just off the top of my head (and untested), but you get the general idea. You should be able to split up the date and reorder the pieces.
Assuming
<xml>
<date>2009-11-18</date>
</xml>
This XSLT 1.0 solution would do it:
<xsl:template match="date">
<xsl:copy>
<xsl:value-of select="
concat(
substring(., 9, 2),
'-',
substring(., 6, 2),
'-',
substring(., 1, 4)
)
" />
</xsl:copy>
</xsl:template>
If your date can be
<xml>
<date>2009-11-1</date>
</xml>
you would have to use the slightly more complicated
<xsl:template match="date">
<xsl:copy>
<xsl:value-of select="
concat(
substring-after(substring-after(., '-'), '-'),
'-',
substring-before(substring-after(., '-'), '-'),
'-',
substring-before(., '-')
)
" />
</xsl:copy>
</xsl:template>
you could also use a template.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:template match="/">
<html>
<body>
<table class="bdr-bot" width="100%" border="0" cellspacing="0" cellpadding="0" style="clear:both">
<tr>
<th width="15%" class="bdr">Date</th>
<th class="bdr">Title</th>
</tr>
<!-- <xsl:for-each select="hexML/body/press_releases/press_release">-->
<xsl:if test="contains(published/#date, '2009')">
<tr>
<td valign="top">
<xsl:call-template name="FormatDate">
<xsl:with-param name="DateTime" select="published/#date"/>
</xsl:call-template>
</td>
<td valign="top">
<a href="result-page.aspx?ResultPageURL={location/#href}">
<xsl:value-of select="headline"/>
</a>
</td>
</tr>
</xsl:if>
<!--</xsl:for-each>-->
</table>
</body>
</html>
</xsl:template>
<xsl:template name="FormatDate">
<xsl:param name="DateTime"/>
<xsl:value-of select="substring($DateTime,9,2)"/>-<xsl:value-of select="substring($DateTime,6,2)"/>-<xsl:value-of select="substring($DateTime,1,4)"/><xsl:text> CET</xsl:text>
</xsl:template>
</xsl:stylesheet>
It appears that you need to use an XSLT 2.0 schema aware processor to get built-in support for what you want to do with the xs:dateTime data type and the format-date function.
See http://www.w3.org/TR/xmlschema-2/#dateTime for the requirements for XSLT 2.0 being able to parse the string you have.
The ·lexical space· of dateTime
consists of finite-length sequences of
characters of the form: '-'? yyyy '-'
mm '-' dd 'T' hh ':' mm ':' ss ('.'
s+)? (zzzzzz)?
See http://www.dpawson.co.uk/xsl/rev2/dates.html#d16685e16 for generating output.
format-date( xs:date(
concat(
substring($d,1,4),
'-',
substring($d,7,2),
'-',
substring($d,5,2))),
'[D01] [MNn] [Y0001]')

Resources