Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated XSLT example to use restnames instead of SQLRest #1330

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 87 additions & 66 deletions distribution/examples/xslt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,110 +5,131 @@ With the XSLTInterceptor you can apply XSLT stylesheets to requests and response

#### RUNNING THE EXAMPLE

At the following URL you will get a REST representation of a customer resource.


http://www.thomas-bayer.com/samples/sqlrest/CUSTOMER/7/


In this example we will transform that representation with the XSLTInterceptor.


Execute the following steps:

1. Go to the `examples/xslt` directory.

2. Execute `service-proxy.bat` or `service-proxy.sh`

2. Open the URL http://www.thomas-bayer.com/samples/sqlrest/CUSTOMER/7/ in your browser.
2. Open the URL https://api.predic8.de/restnames/name.groovy?name=Pia in your browser.

3. Compare it with the response of http://localhost:2000/samples/sqlrest/CUSTOMER/7/
3. Compare it with the response of http://localhost:2000/restnames/name.groovy?name=Pia


### HOW IT IS DONE

The following part describes the example in detail.

First take a look at the proxies.xml file.


```
```xml
<router>
<serviceProxy port="2000">
<response>
<transform xslt="customer2person.xsl" />
</response>
<target host="www.thomas-bayer.com" port="80" />
<transform xslt="./reformat.xsl" />
</response>
<target host="api.predic8.de" port="443" />
</serviceProxy>
</router>
```

You will see that there is a `<serviceProxy>` that directs calls to the port `2000` to www.thomas-bayer.com. Additionally, the `XSLTInterceptor` is set for the rule. The interceptor will be called while processing each request and response.
You will see
that there is a `<serviceProxy>` that directs calls to the port `2000` to [api.predic8.de](https://api.predic8.de).
Additionally, the `XSLTInterceptor` is set for the rule.
The interceptor will be called while processing each request and response.

Now take a closer look at the transform element.

```
<transform xslt="examples/xslt/customer2person.xsl" />
```xml
<transform xslt="./reformat.xsl" />
```

You can reference stylesheets that will be applied to the request and response with the `xslt` attribute. If you leave the attribute blank or do not specify them at all, no transformation will be done. With the element above the interceptor will apply the specified XSLT stylesheet to the response and request. To limit the transformation only to the request or response, use request or response elements to wrap the interceptor. In this example we wrapped the interceptor with a response element so that the transformation is only applied to the response.
You can reference stylesheets that will be applied to the request and response with the `xslt` attribute.
If you leave the attribute blank or do not specify them at all, no transformation will be done.
With the element above, the interceptor will apply the specified XSLT stylesheet to the response and request.
To limit the transformation only to the request or response, use request or response elements to wrap the interceptor.
In this example, we wrapped the interceptor with a response element
so that the transformation is only applied to the response.

```
```xml
<response>
<transform xslt="customer2person.xsl" />
<transform xslt="./reformat.xsl" />
</response>
```

Take a look at the stylesheet:

```
<?xml version="1.0" encoding="UTF-8"?>
```xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/CUSTOMER">
<person>
<name>
<first><xsl:value-of select="FIRSTNAME" /></first>
<last><xsl:value-of select="LASTNAME" /></last>
</name>
<address>
<street><xsl:value-of select="STREET" /></street>
<city><xsl:value-of select="CITY" /></city>
</address>
</person>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/restnames">
<nameinfo>
<name>
<value><xsl:value-of select="nameinfo/name"/></value>
<gender>
<xsl:choose>
<xsl:when test="nameinfo/male = 'true'">male</xsl:when>
<xsl:when test="nameinfo/female = 'true'">female</xsl:when>
</xsl:choose>
</gender>
</name>
<countries>
<xsl:for-each select="nameinfo/countries/country">
<country><xsl:value-of select="."/></country>
</xsl:for-each>
</countries>
</nameinfo>
</xsl:template>
</xsl:stylesheet>
```
We will use the stylesheet to transform the REST resource into another representation. When we open http://www.thomas-bayer.com/samples/sqlrest/CUSTOMER/7/ in our browser, the REST resource will return the following XML document.

```
<?xml version="1.0"?><CUSTOMER xmlns:xlink="http://www.w3.org/1999/xlink">
<ID>7</ID>
<FIRSTNAME>Roger</FIRSTNAME>
<LASTNAME>Seid</LASTNAME>
<STREET>3738 N Monroe St</STREET>
<CITY>Tallahassee</CITY>
</CUSTOMER>
We will use the stylesheet to transform the REST resource into another representation.
When we open https://api.predic8.de/restnames/name.groovy?name=Pia in our browser,
the REST resource will return the following XML document.

```xml
<restnames>
<nameinfo>
<name>Pia</name>
<countries>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Netherlands">Netherlands</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Austria">Austria</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Sweden">Sweden</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Belgium">Belgium</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Frisia">Frisia</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Norway">Norway</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Finland">Finland</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Denmark">Denmark</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Italy">Italy</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Swiss">Swiss</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Germany">Germany</country>
<country href="https://api.predic8.de/restnames/namesincountry.groovy?country=Spain">Spain</country>
</countries>
<gender>female first name</gender>
<male>false</male>
<female>true</female>
</nameinfo>
</restnames>
```

When we open http://localhost:2000/samples/sqlrest/CUSTOMER/7/ in a browser the response is transformed by Membrane and the browser will show the following document:
When we open http://localhost:2000/restnames/name.groovy?name=Piain a browser, the response is transformed by Membrane,
and the browser will show the following document:

```
<?xml version="1.0" encoding="UTF-8"?>
<person>
```xml
<nameinfo>
<name>
<first>Roger</first>
<last>Seid</last>
<value>Pia</value>
<gender>female</gender>
</name>
<address>
<street>3738 N Monroe St</street>
<city>Tallahassee</city>
</address>
</person>
<countries>
<country>Netherlands</country>
<country>Austria</country>
<country>Sweden</country>
<country>Belgium</country>
<country>Frisia</country>
<country>Norway</country>
<country>Finland</country>
<country>Denmark</country>
<country>Italy</country>
<country>Swiss</country>
<country>Germany</country>
<country>Spain</country>
</countries>
</nameinfo>
```
22 changes: 0 additions & 22 deletions distribution/examples/xslt/customer2person.xsl

This file was deleted.

4 changes: 2 additions & 2 deletions distribution/examples/xslt/proxies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<serviceProxy port="2000">
<response>
<transform xslt="./customer2person.xsl" />
<transform xslt="./reformat.xsl" />
</response>
<target host="www.thomas-bayer.com" port="80" />
<target host="api.predic8.de" port="443" />
</serviceProxy>

</router>
Expand Down
27 changes: 27 additions & 0 deletions distribution/examples/xslt/reformat.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/restnames">
<info name="{nameinfo/name}">
<xsl:attribute name="gender">
<xsl:choose>
<xsl:when test="nameinfo/male = 'true'">male</xsl:when>
<xsl:when test="nameinfo/female = 'true'">female</xsl:when>
</xsl:choose>
</xsl:attribute>
<countries>
<xsl:apply-templates select="//country"/>
</countries>
</info>
</xsl:template>

<xsl:template match="country[last()]">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="country">
<xsl:value-of select="."/><xsl:text>, </xsl:text>
</xsl:template>

<xsl:template match="*"/>

</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

public class XSLTTest extends AbstractSampleMembraneStartStopTestcase {

public static final String PATH = "/samples/sqlrest/CUSTOMER/7/";
public static final String PATH = "/restnames/name.groovy?name=Pia";
public static final String CUSTOMER_HOST_LOCAL = "http://localhost:2000";
public static final String CUSTOMER_HOST_REMOTE = "http://www.thomas-bayer.com";
public static final String CUSTOMER_HOST_REMOTE = "https://api.predic8.de";

@Override
protected String getExampleDirName() {
Expand All @@ -32,7 +32,7 @@ protected String getExampleDirName() {

@Test
public void test() throws Exception {
assertContains("FIRSTNAME", getAndAssert200(CUSTOMER_HOST_REMOTE + PATH));
assertContains("first", getAndAssert200(CUSTOMER_HOST_LOCAL + PATH));
assertContains("<male>", getAndAssert200(CUSTOMER_HOST_REMOTE + PATH));
assertContainsNot("<male>", getAndAssert200(CUSTOMER_HOST_LOCAL + PATH));
}
}
Loading