I am working on a project there they have given a list of valid characters which should be passed as it is and any other characters other than them has to be replaced with ?(question mark).
My company software does not allows to use replace function, so i have to use translate method.
I am able to add all the valid characters except '(apostrophe). How to include apostrophe also as a valid character in variable vAllowedSymbols.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:variable name="vAllowedSymbols" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/-?:(),.+ '"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="
translate(
.,
translate(., $vAllowedSymbols, ''),
''
)
"/>
</xsl:template>
Input:
<t>
<Name>#O'Niel</Name>
<Name>St: Peter</Name>
<Name>A.David</Name>
</t>
Output:
<t>
<Name>?O'Niel</Name>
<Name>St: Peter</Name>
<Name>A.David</Name>
</t>
Use
<xsl:variable name="vAllowedSymbols">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/-?:(),.+ '</xsl:variable>
<xsl:variable name="vAllowedSymbols" select='"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/-?:(),.+ '"'/>
There is some advantage in having the variable contain a string instead of a text node. Or more precisely, be a string instead of a node-set that contains a text node.
Related
I'm trying to return the count of names which start with NB but im having trouble as it returns 0. I believe i got the syntax right so it's kind of frustrating to see it return 0. Any help would be appreciated!
XML Input:
<?xml version='1.1' encoding='UTF-8'?>
<breaches>
<breach>
<name>NB111</name>
<severity>MAJOR</severity>
</breach>
<breach>
<name>NB222</name>
<severity>MAJOR</severity>
</breach>
<breach>
<name>NB333</name>
<severity>MAJOR</severity>
</breach>
<breach>
<name>PO999</name>
<severity>MAJOR</severity>
</breach>
</breaches>
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(/breaches/breach/name/*[contains(text(), 'NB')])" />
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>0
I'm trying to return the count of names which start with NB
If you want to count names that start with "NB" then use the starts-with() function, not the contains() function:
<xsl:value-of select="count(breaches/breach[starts-with(name, 'NB')])"/>
This actually counts breach elements that have a child name element that starts with "NB". IOW, it assumes each breach has only one name. If such assumption is incorrect, then use:
<xsl:value-of select="count(breaches/breach/name[starts-with(., 'NB')])"/>
to count the actual name elements.
You could do it like this :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(breaches/breach/name[contains(., 'NB')])" />
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/jxWZS86
I'm using Saxon PE 9.7, XSLT version 3.0.
I try to remove space after <w> before <damage>. I have tried several solutions: normalize-space(), translate(., ' ', ''), even css, white-space: nowrap... I also looked to the solution proposed to How do I remove spaces in all attribute values using xslt?. Unfortunately, none worked.
TEI
<lg>
<!-- other <l> -->
<l>
<!-- other <w> -->
<w xml:id="ktu1-3_ii_l7_ym" type="noun" lemmaRef="uga/noun.xml#ym" rendition="#nowrap">y</w><damage agent="unknown"><supplied resp="KTU" rendition="#bracketBefore #bracketAfter"><w corresp="ktu1-3_ii_l7" type="part-of-noun">m</w></supplied></damage> <!-- type="part-of-noun" because I also have type="part-of-verb", and the display is different -->
</l>
</lg>
When I have damage/supplied/w before the second <w>, it works, but not after the first <w>
XSLT
<xsl:template match="lg/l[#n]">
<li>
<xsl:apply-templates/>
<sup style="font-size: 0.8em">
<xsl:value-of select="#n"/>
</sup>
</li>
</xsl:template>
<xsl:template match="lg/l[#n]/damage/supplied">
<xsl:choose>
<xsl:when test="#rendition">
<xsl:text>[</xsl:text>
<xsl:apply-templates select="./w[not(#rendition='notDisplay')]"/><xsl:text>]</xsl:text>
</xsl:when><xsl:otherwise><xsl:apply-templates select="./w[not(#rendition='notDisplay')]"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="w">
<xsl:apply-templates select="normalize-space(.)"/>
</xsl:template>
<xsl:template match="lg/l[#n]/w">
<xsl:apply-templates select=".[#type= 'noun']" mode="css"/>
<xsl:apply-templates select="normalize-space(.)"/>
</xsl:template>
In advance, thank you for your kind advice.
I'm having trouble working out exactly what you want to achieve. What is the desired output? Are you talking about unwanted space in the visual rendition of the HTML, or about unwanted space in the XML/HTML transformation result?
Writing
<xsl:apply-templates select="normalize-space(.)"/>
is clearly wrong unless you have template rules that match atomic xs:string values, which seems unlikely. And neither of your <w> elements have any whitespace in their string-values, so normalize-space is a no-op anyway.
I want to make a comparison of two different date’s. To realize that, I want to transform to number.
Input example:
<file>
<date>2015-11-06 09:00/>
</file>
<history>
<date>2016-01-12 10:00/>
</history>
First I extract the time, from date. And put the result in a var.
<xsl:for-each select="//item/metadata/document/file/date">
<xsl:variable name="d_log"
select="substring-before(., ' ')" as="xs:string"/>
So the value of the variable would be, for example 2015-11-06.
The next step is, that I want to transform 2015-11-06 in to 20151106.
Questions is how I can transform from a string to a number?
Or is there a easier way?
You can do:
translate(substring-before(., ' '), '-', '')
to get the expected number.
However, your syntax suggests you are using XSLT 2.0. If so, why don't you convert the given strings to xs:date or xs:dateTime and compare them as such?
Here is my result.
<?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"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" indent="yes"></xsl:output>
<xsl:template match="/">
<aaa>
<xsl:for-each select="//item/metadata/document/file/date">
<xsl:variable name="d_log"
select="translate(substring-before(., ' '), '-', '')"
as="xs:string"/>
<xsl:for-each select="../../history/log/date">
<xsl:variable name="d_doc" select="translate(substring-before(., ' '), '-', '')" as="xs:string"/>
<xsl:if test="$d_doc > $d_log">
<bb>
<xsl:value-of select="node()"/>
</bb>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</aaa>
</xsl:template>
</xsl:stylesheet>
I have string like below parsed by an XSLT
boy "happy family" filetype:pdf girl
From the above string, I need to filter out only the single words "boy" and "girl" and get another string as
boy girl
How do I go about achieving this?
With XSLT 2.0 you have the replace function taking a regular expression so doing replace('boy "happy family" filetype:pdf girl', '"[^"]*"|\w+:\w+', '') should work. With XSLT 1.0 I would first check whether your XSLT 1.0 processor support a similar extension function
For XSLT 1.0 there's replace in the http://exslt.org/regular-expressions name space (see http://exslt.org/regexp/functions/replace/index.html) but as Martin pointed out your processor would need support for extensions.
<xsl:value-of select="{http://exslt.org/regular-expressions}replace(STRING, '".*"|\w+:\w+', '')"/>
will give what you asked for.
Here is an XSLT 1.0 solution that uses tokenization provided by FXSL (written itself in XSLT 1.0) and the xxx:node-set() extension function as provided by the XSLT 1.0 processor being used:
<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:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="' '"/>
</xsl:call-template>
</xsl:variable>
<xsl:for-each select=
"ext:node-set($vwordNodes)/*
[not(contains(., '"') or contains(.,':'))
and
count(preceding-sibling::*[contains(., '"')]) mod 2 = 0
]">
<xsl:value-of select="concat(., ' ')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document (the provided text, wrapped in a single top element):
<t>boy "happy family" filetype:pdf girl</t>
the wanted, correct result is produced:
boy girl
The same correct output is produced in the case of the following, more tricky XML document:
<t>boy " very happy family " filetype:pdf girl</t>
I am processing this XML:
<Brand>
<Brand_Name>BLENDERM</Brand_Name>
<Brand_Code>1103</Brand_Code>
<Groups>
<Group>
<Group_Code>657</Group_Code>
<Parent_Code>0</Parent_Code>
<Group_Level>1</Group_Level>
<Group_Name>Brand Default</Group_Name>
<Product>
<Pip_code>0032359</Pip_code>
<Status>In Use</Status>
Using this XSLT:
<xsl:template match="Product" mode="phase-3">
<xsl:value-of select="document('rx_catmapping.xml')/descendant::mapping[source=substring(ancestor::Brand/Brand_Name,1,1)]/target"/>
</xsl:template>
Here is a sample of rx_catmapping.xml:
<Lookup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<mapping>
<source>a</source>
<target>788</target>
</mapping>
<mapping>
<source>B</source>
<target>789</target>
</mapping>
</Lookup>
So, I am processing the Product element, which is a descendant of Brand. The first letter of Brand/Brand_Name in this case is B, and I am trying to output the value 789 by looking it up in rx_catmapping.xml. This should be really simple but I am completely stumped! I have tried changing the first part of the XPath to refer to document('rx_catmapping.xml')/Lookup/mapping, or document('rx_catmapping.xml')//mapping. I have also tried changing the first half of the comparison to string(source), or to source/text(), but neither of these works either. (The reason for trying this was that using source='B', for example, did seem to work, so I wondered if I was trying to compare two incompatible data types.)
Thanks in advance for your help.
Define a key
<xsl:key name="k1" match="mapping" use="source"/>
then use
<xsl:variable name="map-doc" select="document('rx_catmapping.xml')"/>
and
<xsl:variable name="letter" select="substring(ancestor::Brand/Brand_Name,1,1)"/>
<xsl:for-each select="$map-doc">
<xsl:value-of select="key('k1', $letter)/target"/>
</xsl:for-each>
With XSLT 2.0 you can simplify that to
<xsl:value-of select="key('k1', substring(ancestor::Brand/Brand_Name,1,1), $map-doc)"/>
The problem is, I believe, that at the point where you do ancestor::Brand/Brand_Name, the context is the mapping element in the external file. This worked for me
<xsl:template match="/">
<xsl:apply-templates select=".//Product"/>
</xsl:template>
<xsl:template match="Product">
<xsl:variable name="x" select="substring(ancestor::Brand/Brand_Name,1,1)"/>
<xsl:value-of select="document('file:///c:/temp/rx_catmapping.xml')//mapping[source=$x]/target"/>
</xsl:template>