I have an XSL file which uses a a static website link as shown below:
<xsl:template match="my_match">
<xsl:variable name="variable1">
<xsl:value-of select="sel1/Label = 'Variable1'"/>
</xsl:variable>
<xsl:copy-of select="sites:testPath('http://testsite.com/services/testService/v1.0', $fname, $lname,
$email , $zip, $phone, $comments, $jps, boolean($myvar), string(cust/#custID), string(#paID))"/>
</xsl:template>
My question is that how to read a properties file(key value pair) in the xsl file. so in my properties file (e.g. site.properties) I have a key called site i.e. site=testsite.com/services/testService/v1.0
I want to use this site key in place of specifying url value in the xsl i.e. http://testsite.com/services/testService/v1.0. The reason for doing this is that this link changes depending on the various environments.
Is this possible?
Please give your suggestions or a sample code if possible...Also if this is not possible...is there any work-around?
As a proof of concept:
Input .properties file:
# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
website = http://example.com
language = English
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="Functions"
version="2.0">
<xsl:variable name="properties" select="unparsed-text('.properties')" as="xs:string"/>
<xsl:template match="/" name="main">
<xsl:value-of select="f:getProperty('language')"/>
</xsl:template>
<xsl:function name="f:getProperty" as="xs:string?">
<xsl:param name="key" as="xs:string"/>
<xsl:variable name="lines" as="xs:string*" select="
for $x in
for $i in tokenize($properties, '\n')[matches(., '^[^!#]')] return
tokenize($i, '=')
return translate(normalize-space($x), '\', '')"/>
<xsl:sequence select="$lines[index-of($lines, $key)+1]"/>
</xsl:function>
</xsl:stylesheet>
The f:getProperty('language') will return 'English'.
See this as a proof of concept, this needs to be improved in many ways since it does not handle many of the different ways a .properties file can be authored.
I belive Alejandro or Dimitrie probably could improve this many times.
For an XSLT 1.0 solution, you could use an external (parsed) general entity in an XML file that will load the properties file as part of the XML content.
For example, if you had a properties file like this, named site.properties:
foo=x
site=http://testsite.com/services/testService/v1.0
bar=y
You could create a simple XML file, named properties.xml that "wraps" the content of the properties file and loads it using an external parsed general entity:
<!DOCTYPE properties [
<!ENTITY props SYSTEM "site.properties">
]>
<properties>
&props;
</properties>
Then, within your XSLT you can load that properties.xml using the document() function and obtain the value for a given key:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="props" select="document('properties.xml')" />
<xsl:template match="/">
<output>
<example1>
<!--simple one-liner -->
<xsl:value-of select="substring-before(
substring-after($props,
concat('site','=')),
'
')" />
</example1>
<example2>
<!--using a template to retrieve the value
of the "site" property -->
<xsl:call-template name="getProperty">
<xsl:with-param name="propertiesFile" select="$props"/>
<xsl:with-param name="key" select="'site'"/>
</xsl:call-template>
</example2>
<example3>
<!--Another example using the template to retrieve
the value of the "foo" property,
leveraging default param value for properties -->
<xsl:call-template name="getProperty">
<!--default $propertiesFile defined in the template,
so no need to specify -->
<xsl:with-param name="key" select="'foo'"/>
</xsl:call-template>
</example3>
</output>
</xsl:template>
<!--Retrieve a property from a properties file by specifying the key -->
<xsl:template name="getProperty">
<xsl:param name="propertiesFile" select="$props"/>
<xsl:param name="key" />
<xsl:value-of select="substring-before(
substring-after($propertiesFile,
concat($key,'=')),
'
')" />
</xsl:template>
</xsl:stylesheet>
When applied to any XML input the stylesheet above will produce the following output:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<example1>http://testsite.com/services/testService/v1.0</example1>
<example2>http://testsite.com/services/testService/v1.0</example2>
<example3>x</example3>
</output>
Note: this strategy will only work if the content of the properties file is "XML safe". If it were to contain characters, like & or < it would result in an XML parsing error when the properties.xml file is loaded.
Related
Can anyone please help me on masking of non-XML input in Datapower.
I am logging the input first in Datapower log store and taking that input to mask, I have written a piece of code which works for XML but not for non-XML,
attaching my input non-XML and code for the reference.
Attached masking file.xsl has two templates, one for non-XML and another for XML. XML is working fine for numeric values and not masking the characters.
Non-XML template is called but not doing masking.(I can see in system logs it is being called)
The control given to masking template is given by a different log.xml file attached.
Quick response is appreciated.
Thanks,
Anuj
Input Non-xml.
PCTM-ODS-MTRF-WRSP-RSP PCTM-ODS-MTRF-WRSP-RSP 0200000298111 00000000ODS00000000000000834978953 00LIQ055003241NYYNYNNY 10000020130823Y000000000018765-000000000018765-000000000018765-010000100000000000000000000000-000000000000000-000000000000000-
My Code: In the below maskFldName is called from the log.xml file.
<xsl:template name="NONXMLmaskingTemplate">
<xsl:param name="maskFldName"/>
<xsl:param name="Input"/>
<xsl:variable name="maskchars" select="'******************************'"/>
<xsl:message>maskFldName:<xsl:copy-of select="$maskFldName"/>
</xsl:message>
<xsl:variable name="logInput">
<!--<dp:serialize select="$Input" omit-xml-decl="yes"/>-->
<xsl:copy-of select="translate($Input,'" : "','":"')"/>
</xsl:variable>
<xsl:message>MaskInputMessage:<xsl:copy-of select="$logInput"/>
</xsl:message>
<dp:set-local-variable name="'var://local/input'" value="$logInput"/>
<xsl:message>!!!!
<xsl:copy-of select="translate($Input,' ','')"/>
</xsl:message>
<xsl:for-each select="$maskFldName">
<xsl:variable name="startPosition" select="./#startPosition"/>
<xsl:variable name="noOfChars" select="./#numOfChars"/>
<xsl:message>startPosition:<xsl:value-of select="$startPosition"/>
</xsl:message>
<xsl:message>noOfChars:<xsl:value-of select="$noOfChars"/>
</xsl:message>
<xsl:variable name="maskString" select="substring($maskchars,1,$noOfChars)"/>
<xsl:message>Matches:<xsl:copy-of select="regexp:match($logInput,.,'g')"/>
</xsl:message>
<xsl:message>InputMessage1:<xsl:copy-of select="$Input"/>
</xsl:message>
<xsl:for-each select="regexp:match($logInput,.,'g')">
<xsl:message>InputMessage:<xsl:copy-of select="$logInput"/>
</xsl:message>
<xsl:message>ValueX:<xsl:copy-of select="."/>
</xsl:message>
<xsl:variable name="strToReplace" select="substring(.,$startPosition,$noOfChars)"/>
<xsl:variable name="strToReplace2" select="regexp:replace(.,$strToReplace,'g',$maskString)"/>
<xsl:message>strToReplace:<xsl:copy-of select="$strToReplace"/>
</xsl:message>
<xsl:message>strToReplace2:<xsl:value-of select="$strToReplace2"/>
</xsl:message>
<dp:set-local-variable name="'var://local/input'" value="regexp:replace(dp:local-variable('var://local/input'),.,'g',$strToReplace2)"/>
</xsl:for-each>
</xsl:for-each>
<xsl:copy-of select="dp:local-variable('var://local/input')"/>
<!--
If message to be proper xml i.e. < needs to be output as < then the following code to be used
<xsl:copy-of select="dp:transform('detailLog.xsl',dp:parse(dp:local-variable('var://local/input')))"/>
<xsl:message>MaskOutputMessage:<xsl:copy-of select="dp:local-variable('var://local/input')"/> </xsl:message>
-->
</xsl:template>
</xsl:stylesheet>
log.xml
<?xml version="1.0" encoding="UTF-8"?>
<logConfig>
<!-- ALL/NONE/FRONT/BACK -->
<log-enabled value="ALL"/>
<!-- Y/N -->
<detail-log-enabled value="Y"/>
<!-- Y/N -->
<slim-log-enabled value="N"/>
<masking isMaskRequire="Y">
<logpoint id="service_req_entry">
<contentType>NONXML</contentType>
<maskPattern attribute="detail" startPosition="50" numOfChars="4">(PCTM[\w])</maskPattern>
</logpoint>
<logpoint id="SCRq-out">
<contentType>XML</contentType>
<maskPattern attribute="detail" startPosition="11" numOfChars="4">(Severity>[\w]+<)</maskPattern>
</logpoint>
</masking>
</logConfig>
Perhaps you could use this technote to wrap the non-xml in XML and then use your XSLT
http://www-01.ibm.com/support/docview.wss?uid=swg21321379
Relevant portion:
Transforming raw text to pseudo-XML:
This example will take the following text as input:
some text here
and here is some more
and some more
and some more
and wait, some more here
Steps to Create:
1.Create a Transform action in the service that will be receiving non-XML requests.
2.Select "Use XSLT specified in this action on a non-XML message" to specify that this is a Binary Transform.
3.Upload the following style sheet as the Processing Control File.
4.Click Done, and Apply all windows.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
version="1.0">
<dp:input-mapping href="sample.ffd" type="ffd"/>
<!-- This stylesheet copies the input to the output -->
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:value-of select="sampleFile/sampleTag" disable-output-escaping="yes" />
</xsl:template>
</xsl:stylesheet>
Upload the following FFD file into the local://sample.ffd directory from the File Manager.
<File name="sampleFile">
<!-- capture all data into this tag -->
<Field name="sampleTag" />
</File>
Sending the sample input above will result in the following output
<?xml version="1.0" encoding="utf-8"?>
<sampleFile>
<sampleTag>
some text here
and here is some more
and some more
and some more
and wait, some
</sampleTag>
</sampleFile>
I am newbie to XSLT.
I have a requirement to read a URL and convert some of its values into XML.
I wrote a XSLT that has to take URL as the input value and create a XML file from some of the content of the URL value.
When I debugged the XSLT in XMLSPY, I noticed that the URL value is not being picked up by inputValue variable in the below code. I am not sure if my approach to input the URL and the template match are wrong.
Any help is appreciated.
Thanks in advance.
Input to XSLT:
http://host:port/abc/xyz1/6xyz6?qq=123&pp=3
Here the XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:nnc="Nnc" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:param name="inVal" select="xs:string(http://host:port/abc/xyz1/6xyz6?qq=123&pp=3)"/>
<xsl:template match="/">
<xsl:variable name="inputValue" select="$inVal"/>
<xsl:if test="string-length($inputValue)=0">
<xsl:message terminate="yes">
inputValue is blank
</xsl:message>
</xsl:if>
<xsl:variable name="value" as="xs:string" select="substring-after($inputValue, 'abc/' )"/>
<xsl:variable name="tokenizedValues" select="tokenize($value,'/')"/>
<xsl:for-each select="$tokenizedValues">
<xsl:if test="position() = 1">
<id>
<xsl:value-of select="."/>
</id>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The desired XML output:
<?xml version="1.0" encoding="UTF-8"?>
<id>6xyz6</id>
<qq>123</qq>
<pp>123</pp>
Well if you want to pull in a text file then with XSLT 2.0 and later you can do that but not by simply using a URL, you need to call the unparsed-text function e.g.
<xsl:variable name="inputData" as="xs:string" select="unparsed-text('http://example.com/foo')"/>
See http://www.w3.org/TR/xslt20/#unparsed-text, depending on the encoding of your text document you need to add a second parameter when calling the function.
Is there a way pass a attribute, variable combinations directly into the path of the url with xsl?
Example:
http://something.xsl?asdf=12&attribute2=1234
I would like to use these attributes and values to enable certain flags inside the xsl file.
Use concat()
<xsl:variable name="url" select="concat($currURL, 'flag=true')" />
Yeah, you just need to escape the & as &
I think you mean can the stylesheet access its own URI to access the parameters. In XSLT2 you can use the static-base-uri() function to access the URI, and then you can split it up to extract the query parameters using the regex string functions. In XSLT1 it is not possible, you would need to pass the information in as stylesheet parameters, and XSLT1 stylesheet has no access to the URI of the source or itself.
Here is a complete XSLT 1.0 solution, assuming that the URL is passed as an external parameter to the transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pUrl" select=
"'http://something.xsl?asdf=12&attribute2=1234'"/>
<xsl:template match="/">
<xsl:variable name="vQuery" select="substring-after($pUrl, '?')"/>
<xsl:variable name="vrtfQueryItems">
<xsl:call-template name="buildQueryItems">
<xsl:with-param name="pQuery" select="$vQuery"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vQueryItems"
select="ext:node-set($vrtfQueryItems)/*"/>
<xsl:copy-of select="$vQueryItems"/>
</xsl:template>
<xsl:template name="buildQueryItems">
<xsl:param name="pQuery"/>
<xsl:if test=
"string-length($pQuery) > 0">
<xsl:variable name="vQuery" select="concat($pQuery, '&')"/>
<xsl:variable name="vItem" select="substring-before($vQuery, '&')"/>
<param name="{substring-before(concat($vItem, '='), '=' )}">
<xsl:value-of select="substring-after($vItem, '=')"/>
</param>
<xsl:call-template name="buildQueryItems">
<xsl:with-param name="pQuery" select="substring-after($pQuery, '&')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the wanted result is produced:
<param name="asdf">12</param>
<param name="attribute2">1234</param>
I'm trying out a sample of look-up tables in XSLT and am not able to get it to work
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />
<xsl:key name="classification-lookup" match="classification" use="id" />
<xsl:variable name="classification-top" select="document('')/*/classifications" />
<xsl:template match="BusinessListing">
<listing>
<id>
<xsl:value-of select="id" />
</id>
<xsl:apply-templates select="$classification-top">
<xsl:with-param name="curr-label" select="." />
</xsl:apply-templates>
</listing>
</xsl:template>
<xsl:template match="classifications">
<xsl:param name="curr-label" />
<category>
<xsl:value-of select="key('classification-lookup', $curr-label/listingData/classifications/classificationId)/description" />
</category>
</xsl:template>
<classifications>
<classification>
<id>7981</id>
<description>Category1</description>
</classification>
<classification>
<id>7982</id>
<description>Category2</description>
</classification>
<classification>
<id>7983</id>
<description>Category3</description>
</classification>
<classification>
<id>7984</id>
<description>Category4</description>
</classification>
</classifications>
</xsl:stylesheet>
and the source is as below .
<?xml version="1.0"?>
<BusinessListings>
<BusinessListing>
<id>1593469</id>
<listingData>
<classifications>
<classificationId>7982</classificationId>
<classificationId>7983</classificationId>
</classifications>
</listingData>
</BusinessListing>
</BusinessListings>
In the result below , The category is empty but I need the Classification Id from the source to be matched with the id in the classification tag and the category generated .
<?xml version="1.0" encoding="UTF-8"?>
<listing>
<id>1593469</id> -- Empty I need the Category2 and Category3 here
<category/>
</listing>
I know that I may be wide off mark but I've just started off with XSLT and referred the sample here http://www.ibm.com/developerworks/xml/library/x-xsltip.html . Thanks for the help .
Your XSLT stylesheet contains an error -- according to spec, any child-element of xsl:stylesheet (aka top-level element) must be in a non-null namespace:
"*In addition, the xsl:stylesheet
element may contain any element not
from the XSLT namespace, provided that
the expanded-name of the element has a
non-null namespace URI. "
If the XSLT processor you are using doesn't raise an error, then it is non-compliant and buggy and shouldn't be used. Find and use a compliant XSLT processor (I am using .NET XslCompiledTransform, Saxon 6.5.5, ..., etc).
There are other errors, too.
Solution:
Define a new namespace with prefix (say) "x:":
Change the embedded <classifications> to <x:classifications> -- now this conforms to the Spec.
Perform more changes to the code until you get this transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="my:x" exclude-result-prefixes="x">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="classification-lookup" match="classification"
use="id" />
<xsl:template match="BusinessListing">
<listing>
<id>
<xsl:value-of select="id" />
</id>
<xsl:apply-templates/>
</listing>
</xsl:template>
<xsl:template match="classificationId">
<xsl:variable name="vCur" select="."/>
<xsl:for-each select="document('')">
<category>
<xsl:value-of select=
"key('classification-lookup',$vCur)/description" />
</category>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>
<x:classifications>
<classification>
<id>7981</id>
<description>Category1</description>
</classification>
<classification>
<id>7982</id>
<description>Category2</description>
</classification>
<classification>
<id>7983</id>
<description>Category3</description>
</classification>
<classification>
<id>7984</id>
<description>Category4</description>
</classification>
</x:classifications>
</xsl:stylesheet>
.4. In the above code notice the line: <xsl:for-each select="document('')"> .
The purpose of this is to make the stylesheet the current document. The key() function operates only on the current document and if you want the embedded classification elements to be indexed and used, you must change the current document (usually in this way). In XSLT 2.0 the key() function allows a 3rd argument which is a node from the document whose index should be used.
When this transformation is applied to the provided XML document:
<BusinessListings>
<BusinessListing>
<id>1593469</id>
<listingData>
<classifications>
<classificationId>7982</classificationId>
<classificationId>7983</classificationId>
</classifications>
</listingData>
</BusinessListing>
</BusinessListings>
the wanted, correct result is produced:
<listing>
<id>1593469</id>
<category>Category2</category>
<category>Category3</category>
</listing>
I've the following xslt file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- USDomesticCountryList - USE UPPERCASE LETTERS ONLY -->
<xsl:variable name="USDomesticCountryList">
<entry name="US"/>
<entry name="UK"/>
<entry name="EG"/>
</xsl:variable>
<!--// USDomesticCountryList -->
<xsl:template name="IsUSDomesticCountry">
<xsl:param name="countryParam"/>
<xsl:variable name="country" select="normalize-space($countryParam)"/>
<xsl:value-of select="normalize-space(document('')//xsl:variable[#name='USDomesticCountryList']/entry[#name=$country]/#name)"/>
</xsl:template>
</xsl:stylesheet>
I need to replace the "document('')" xpath function, what should I use instead?
I've tried to remove it completely but the xsl document doesn't work for me!
I need to to so because the problem is :
I am using some XSLT document that uses the above file, say document a.
So I have document a that includes the above file (document b).
I am using doc a from java code, I am do Caching for doc a as a javax.xml.transform.Templates object to prevent multiple reads to the xsl file on every transformation request.
I found that, the doc b is re-calling itself from the harddisk, I believe this is because of the document('') function above, so I wanna replace/remove it.
Thanks.
If you want to access the nodes inside a variable you normally use the node-set() extension function. The availability and syntax depends on the processor you use. For MSXML and Saxon you can use exsl:node-set(). To use the extension function you will have to include the namespace that defines the function.
E.g. (tested wiht MSXML, returns US for countryName = 'US'):
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
>
<xsl:output method="xml"/>
<!-- USDomesticCountryList - USE UPPERCASE LETTERS ONLY -->
<xsl:variable name="USDomesticCountryList">
<entry name="US"/>
<entry name="UK"/>
<entry name="EG"/>
</xsl:variable>
<!--// USDomesticCountryList -->
<xsl:template name="IsUSDomesticCountry">
<xsl:param name="countryParam"/>
<xsl:variable name="country" select="normalize-space($countryParam)"/>
<xsl:value-of select="exsl:node-set($USDomesticCountryList)/entry[#name=$country]/#name"/>
</xsl:template>
</xsl:stylesheet>
If you're trying to make the IsUSDomesticCountry template work without using document(''), you could rewrite the template to
<xsl:template name="IsUSDomesticCountry">
<xsl:param name="countryParam"/>
<xsl:variable name="country" select="normalize-space($countryParam)"/>
<xsl:choose>
<xsl:when test="$country='US'">true</xsl:when>
<xsl:when test="$country='UK'">true</xsl:when>
<xsl:when test="$country='EG'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:template>
or
<xsl:template name="IsUSDomesticCountry">
<xsl:param name="countryParam"/>
<xsl:variable name="country" select="normalize-space($countryParam)"/>
<xsl:value-of select="$country='US' or $country='UK' or $country='EG'"/>
</xsl:template>
or even
<xsl:template name="IsUSDomesticCountry">
<xsl:param name="countryParam"/>
<xsl:variable name="country"
select="concat('-', normalize-space($countryParam),'-')"/>
<xsl:variable name="domesticCountries" select="'-US-UK-EG-'"/>
<xsl:value-of select="contains($domesticCountries, $country)"/>
</xsl:template>
Personally, I find the variant using document('') to be more readable.