Given the following input xml file, i need to search for values, and replace them in the input xml
the search for values are in the xslt, each row must be replaced by its equivalent row.
<TABLE NAME="TEST">
<DATA RECORDS="78">
<catalog>
<book id="bk109">
<description>2ος Όροφος</description>
</book>
<book id="bk110">
<description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<description>An anthology of HORROR stories about roaches, centipedes, scorpions and other insects.</description>
</book>
<book id="bk112">
<description>2ος όροφος</description>
</book>
<book id="bk113">
<description>An anthology of horror stories about roaches, centipedes, scorpions and other insects.</description>
</book>
<book id="bk114">
<description>Microsoft's .NET initiative is explored in detail in this deep PROGRAMMER's reference.</description>
</book>
<book id="bk115">
<description>An anthology of HORROR stories about roaches, centipedes, scorpions and other insects.</description>
</book>
<book id="bk116">
<description>An anthology of horror stories about roaches, centipedes, scorpions and other insects. Beware, this must not be matched.</description>
</book>
<book id="bk114">
<description>Microsoft's .NET initiative is explored in detail in this deep PROGRAMMER's reference. Beware, this must not be matched.</description>
</book>
</catalog>
</DATA>
</TABLE>
and the following xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:functx="http://www.functx.com"
exclude-result-prefixes="xs functx">
<xsl:param name="search-text" as="xs:string">2ος Όροφος
2ος όροφος</xsl:param>
<xsl:param name="replacement-text" as="xs:string">2ος όροφoς
2ος όροφoς</xsl:param>
<xsl:param name="search-terms" as="xs:string*" select="tokenize($search-text, '\r?\n')"/>
<xsl:param name="search-terms-is" as="xs:string*" select="for $term in $search-terms return concat('^', lower-case(functx:escape-for-regex($term)), '$')"/>
<xsl:param name="replace-terms" as="xs:string*" select="tokenize($replacement-text, '\r?\n')"/>
<xsl:include href="http://www.xsltfunctions.com/xsl/functx-1.0-nodoc-2007-01.xsl"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="description[some $search-term in $search-terms-is satisfies matches(., $search-term, 'i')]">
<xsl:copy>
<xsl:variable name="matched-term" as="xs:string" select="$search-terms-is[matches(current(), ., 'i')]"/>
<xsl:variable name="replacement" as="xs:string" select="$replace-terms[index-of($search-terms-is, $matched-term)]"/>
<xsl:value-of
select="$replacement"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
i get the errors.
How could the above be altered, so as to keep all the code the xslt has, but avoid the following error?
<?xml version="1.0" encoding="UTF-8"?><TABLE NAME="TEST">
<DATA RECORDS="78">
<catalog>
<book id="bk109">
Error on line 30
XTTE0570: A sequence of more than one item is not allowed as the value of variable
$matched-term ("^2ος όροφος$", "^2ος όροφος$")
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]/catalog[1]/book[1]/description[1]
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]/catalog[1]/book[1]
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]/catalog[1]
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]
in built-in template rule
live here:
http://xsltransform.net/pNvs5wd
Also i would appreciate an xslt, where the search and replace values could be loaded in external files. ie search.txt, replace.txt
The posted code assumes any search term is entered once and is the converted by the XSLT to lower case in <xsl:param name="search-terms-is" as="xs:string*" select="for $term in $search-terms return concat('^', lower-case(functx:escape-for-regex($term)), '$')"/>. Furthermore the matching of input data with the search terms in some $search-term in $search-terms-is satisfies matches(., $search-term, 'i') is done using the i flag to use case-insensitive matching. Your two terms 2ος Όροφος and 2ος όροφος differ only in the case of characters so they both result in a match of the input data. As the variable storing the match is type as xs:string expecting only a single match you get that error.
To fix that you should either make sure your search terms contain a certain term only once or you need to remove the use of lower-case and the i flag.
As for using text files with the search and replacement list, XSLT 2 and later supports the XPath 2 and later unparsed-text(file-uri, optional-encoding) function you can use as in
<xsl:param name="searchFile" as="xs:string">search-terms.txt</xsl:param>
<xsl:param name="search-text" as="xs:string" select="unparsed-text($searchFile)"/>
to load the terms from a text file.
Related
Here is my sample input xml
<?xml version="1.0" encoding="UTF-8"?>
<Update xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Request>
<List>
<RequestP><ManNumber>3B4</ManNumber></RequestP>
<RequestP><ManNumber>8T7_BE</ManNumber></RequestP>
<RequestP><ManNumber>3B5</ManNumber></RequestP>
<RequestP><ManNumber>5E9_BE</ManNumber></RequestP>
<RequestP><ManNumber>9X6</ManNumber></RequestP>
</List>
</Request>
</Update>
and xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" exclude-result-prefixes="#all">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
<xsl:mode streamable="yes" />
<xsl:template match="List/RequestP/ManNumber">
<ManNumber>
<xsl:value-of select="replace(.,'_BE','')" />
</ManNumber>
<xsl:if test="contains(.,'_BE')">
<ManDescrip>BE</ManDescrip>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I am getting below error for above xslt, I am using Saxon 11.2 version
Template rule is not streamable
* There is more than one consuming operand: {<ManNumber {xsl:value-of}/>} on line 6, and {if(fn:contains(...)) then ... else ...} on line 9
The xslt works fine if I use either "replace" or "contains" but not both within same template.
Streamed processing, if you have needs (huge input documents in the size of gigabytes) to use it, requires you to limit your XSLT to streamable code, that means you can for instance make a copy of that element and processed only that small element node as a complete in memory element in a different mode
<xsl:template match="List/RequestP/ManNumber">
<xsl:apply-templates select="copy-of(.)" mode="grounded"/>
</xsl:template>
<xsl:template name="grounded" match="ManNumber">
<ManNumber>
<xsl:value-of select="replace(.,'_BE','')" />
</ManNumber>
<xsl:if test="contains(.,'_BE')">
<ManDescrip>BE</ManDescrip>
</xsl:if>
</xsl:template>
I need to localize values within siblings that are the same. If they are the same I need to alter them.
I think I need to use following-sibling and preceding-sibling and group-by in some way. First group-by the value I am looking for so that I get the one's that are the same in the position after each other. Then using the sibling functions to find out if they are equal.
Sample:
<programs>
<event>
<start>2018-11-25T13:55:00</start>
</event>
<event>
<start>2018-11-27T17:00:00</start>
</event>
<event>
<start>2018-11-25T13:55:00</start>
</event>
<event>
<start>2018-11-25T13:55:00</start>
</event>
</programs>
Code:
<?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:template match="/">
<output>
<xsl:for-each select="/programs/event">
<xsl:variable name="starttime" select="./start"/>
<startOfProgram><xsl:value-of select="$starttime"/></startOfProgram>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Desired results:
<output>
<startOfProgram>2018-11-25T13:55:00</startOfProgram>
<startOfProgram>2018-11-25T13:56:00</startOfProgram>
<startOfProgram>2018-11-25T13:57:00</startOfProgram>
<startOfProgram>2018-11-27T17:00:00</startOfProgram>
</output>
I know this is a long shot so if anyone could point me in the right direction or help me with one part of the problem I'd be very grateful.
There is lots of other elements in the sample that I have taken out that is also carried though to the output. If it matters I can include a variety of them.
Ps. Note that the value could easily be 2018-11-25T18:30:00, which would then need to be 2018-11-25T18:30:00 and the consecutive 2018-11-25T18:31:00 if there are more of the same.
The result you have shown looks as if you want to group the values as xs:dateTime values and then simply add one minute to each item in the group depending on the position:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="programs">
<output>
<xsl:for-each-group select="event/start/xs:dateTime(.)" group-by=".">
<xsl:for-each select="current-group()">
<startOfProgram>{. + (position() - 1) * xs:dayTimeDuration('PT1M')}</startOfProgram>
</xsl:for-each>
</xsl:for-each-group>
</output>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/pPqsHUv/1 and the above is XSLT 3 but for an XSLT 2 processor I think you only need to change the text value template I have used to an xsl:value-of:
<startOfProgram><xsl:value-of select=". + (position() - 1) * xs:dayTimeDuration('PT1M')"/></startOfProgram>
See http://xsltransform.hikmatu.com/6qVRKvJ
I have the following snippet
<xsl:for-each select="book">
<xsl:value-of select="title"/><xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
I want it to be a list of comma-separated book titles.
The output is correct when I have no books in the list. It is also correct when I have more than one book.
However, if I have exactly one book, it prints a comma at the end.
When I change the condition to only print a comma if it is the last element, the case where there is only one book doesn't print a comma.
So it seems like when there is only one element it is not treated as the last element. How can I deal with this?
Sample XML: looks something like this
<booklist>
<book>
<title>My book</title>
<author>Some author</author>
</book>
<book>
<title>Another book</title>
<author>Another author</author>
</book>
</booklist>
When there are two books in the list, I get
My book, Another book
When I delete the second entry, I get
My book,
EDIT:
I found the issue. I was using an <xsl:apply-templates> to select a set of data, and then inside another template for each book, I was doing the position check. However, last() was referring to the original set of data and not the filtered subset.
The following works fine for me in Firefox (I get no commas for no books, and n-1 commas for n books if n>0). I suspect your XSLT processor is broken.
The XSLT ("x.xsl"):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="iso-8859-1" media-type="text/xhtml" doctype-public="-//W3C//DTD HTML 4.0//EN"/>
<xsl:template match="/">
<xsl:for-each select="root/book">
<xsl:value-of select="title"/><xsl:if test="position() != last()">;</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="x.xsl" type="text/xsl"?>
<root>
<book>
<title>A</title>
</book>
<book>
<title>B</title>
</book>
</root>
What's "test()"? I think you want "last()-1".
I'm trying to find a way to replace a node with one that has the same name deeper down in the tree. For example, with the following input:
<root>
<foo>
<a>1</a>
<b>2</b>
<c>3</c>
<bar>
<a>100</a>
<c>5000</c>
</bar>
</foo>
</root>
I'd like to produce something like this:
<root>
<foo>
<a>100</a>
<b>2</b>
<c>5000</c>
</foo>
</root>
I need to be able to replace any number of nodes, and I'd also like to figure out the list dynamically, rather than spell out all the possibilities because there's a chance that things will change in the future. One other requirement is that order of the parent nodes must remain intact. (To be specific, my final output is going to be a CSV file so the columns need to line up with the headers.)
This is my first attempt at learning XSLT and I'm totally stumped on this one! Any help would be greatly appreciated. I'm using XSLT 2.0, BTW.
Thanks,
Mark
I apologize for the nasty SO bug which doesn't indent the formatted code!
They can't fix this for months...
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"*[*]
[every $leaf in .//*[not(*)]
satisfies
name($leaf) = preceding::*/name()
]
"/>
<xsl:template match=
"*[not(*) and name() = following::*/name()]">
<xsl:sequence select=
"following::*[name() = name(current())][1]"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<root>
<foo>
<a>1</a>
<b>2</b>
<c>3</c>
<bar>
<a>100</a>
<c>5000</c>
</bar>
</foo>
</root>
produces the wanted, correct result:
<root>
<foo>
<a>100</a>
<b>2</b>
<c>5000</c>
</foo>
</root>
See Diff 1.0 vs 2.0. That one is solved, but it is still a bit of a mystery to me what caused the issue in the first place.
Now I may have found something, but need help understanding what is going on.
I simplified the input xml to
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Manager>
<Employee grade="9"/>
<Employee grade="8"/>
</Manager>
<Manager>
<Employee grade="9"/>
<Employee grade="8"/>
<Employee grade="4"/>
</Manager>
</root>
The stylesheet I apply on this input is
<?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:template match="/">
<root>
<xsl:apply-templates select="root/Manager"/>
</root>
</xsl:template>
<xsl:template match="Manager">
<test><xsl:value-of select="Employee/#grade"/></test>
</xsl:template>
</xsl:stylesheet>
The output is
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>9</test>
<test>9</test>
</root>
But running the transformation in XSLT 2.0 mode (change stylesheet/#version to "2.0"), the output is
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>9 8</test>
<test>9 8 4</test>
</root>
I wonder what precise difference in XSLT 1.0 and XSLT 2.0 causes this.
Well for the first difference I did explain that with XSLT 2.0 the comparison operators like less than or greater than or less than or equal and so on by default compare strings while with XSLT 1.0 these operators are only defined for numbers and that way convert any operands to numbers.
For this post the difference is simply that with XSLT 1.0 xsl:value-of select="foo" outputs the string value of the first foo element in the selected node set of foo elements while with XSLT 2.0 this has changed, if a sequence is selected then a space separated list of the string value of item in the sequence is output. You can change the separator (i.e. space) used with the separator attribute of xsl:value-of in XSLT 2.0. See also http://www.w3.org/TR/xslt20/#incompatibilities.