collection() function does not encount all 50 letters, only the one on which the transformation is done.
I have to transform many xml-letters in a csv file with the parameters "Key", "bezeichnung", and "reference" from any letter located in the same folder. When I execute the transformation, I get only the data from the actually transformed file and not from all 50 letters. What is the problem here with the collection() function because everything else works
<xsl:variable name="briefe"
select="collection('./?select=l_*.xml')"/>
<xsl:template match="/">
<xsl:text>"Key","Bezeichnung","Referenz"</xsl:text>
<xsl:text>
</xsl:text>
<xsl:for-each-group select="//tei:correspAction[#type='received']/tei:placeName" group-by="#key">
<xsl:sort select="." />
<xsl:text>"</xsl:text><xsl:value-of select="current-grouping-key()" /><xsl:text>",</xsl:text>
<xsl:text>"</xsl:text><xsl:value-of select="distinct-values(//tei:correspAction[#type='received']/tei:placeName)" /><xsl:text>",</xsl:text>
<xsl:text>"</xsl:text><xsl:value-of select="//tei:correspAction[#type='received']/tei:placeName/#ref" /><xsl:text>"</xsl:text>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Related
My environment is SAXON (last nights build) using XSLT 2.0. My real problem is that the XML document specification is sub-optimal, and in a way, my problem relates to fixing/working around that design issue.
I have a node type (<weaponmodesdata>) where all the direct children are |-separated string lists of 1-or-many elements (each child of the same <weaponmodesdata> will have the same length). I need to go over the various modes represented and "unspin" them out to separate item lists (in plain text), rather than having them all smooshed together.
Unfortunately right now I'm getting a really stubborn
XPTY0020: Required item type of the context item for the child axis is node(); supplied
value has item type xs:string
error on the lines where I pass the node that needs to be split up into my little template.
Currently I have
<xsl:template match="trait" mode="attack">
<xsl:for-each select="tokenize(weaponmodesdata/mode, '\|')">
<xsl:variable name="count" select="position()"/>
<xsl:value-of select="name"/><xsl:text> - </xsl:text>
<xsl:call-template name="split_weaponmode">
<xsl:with-param name="source" select="weaponmodesdata/damage"/>
<xsl:with-param name="item" select="$count"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="split_weaponmode">
<xsl:with-param name="source" select="weaponmodesdata/damtype"/>
<xsl:with-param name="item" select="$count"/>
</xsl:call-template>
<!-- more will go here eventually -->
<xsl:text>.
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="split_weaponmode">
<xsl:param name="source"/>
<xsl:param name="item"/>
<xsl:variable name="parts" select="tokenize($source, '\|')"/>
<xsl:for-each select="$parts">
<xsl:if test="position() = $item">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
An example XML subtree relating to my issue:
<character>
<trait id="1">
<name>Spear</name>
<weaponmodesdata>
<mode>1H Thrust|2H Thrust|Thrown</mode>
<damage>thr+2|thr+3|thr+3</damage>
<damtype>imp|imp|imp</damtype>
</weaponmodesdata>
</trait>
<trait id="2">
<name>Broadsword</name>
<weaponmodesdata>
<mode>1H Thrust|1H Swing</mode>
<damage>thr+1|sw+2</damage>
<damtype>imp|cut</damtype>
</weaponmodesdata>
</trait>
</character>
Example desired output:
Spear - 1H Thrust; thr+2 imp.
Spear - 2H Thrust; thr+3 imp.
Spear - Thrown; thr+3 imp.
Broadsword - 1H Thrust; thr+1 imp.
Broadsword - 1H Swing; sw+2 cut.
One issue (that one causing the error message) with your code is that your for-each operates on a sequence of string value (i.e. inside the for-each body the context item is a string value), yet you have relative XPath expressions like weaponmodesdata/damage that require a context node to makes sense. So you would need to use a variable outside of the for-each to store your context node.
But I think you can simplify your code to
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="trait">
<xsl:variable name="this" select="."/>
<xsl:variable name="count" select="count(tokenize(weaponmodesdata/*[1], '\|'))"/>
<xsl:for-each-group select="weaponmodesdata/*/tokenize(., '\|')" group-by="position() mod $count">
<xsl:value-of select="$this/name"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="current-group()"/>
<xsl:text>.
</xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
If you want to stick with your approach of calling templates then make sure you store the context node of the template using e.g. <xsl:variable name="this" select="."/> so that you can access it inside of the for-each iterating over a string item.
My output type is text.
I am preparing for Reports.
My text output got to accept only 50 character width after that which has to be wrapped in to the next line.
I have a solution to line wrap for the elements in the text.
Is there any way to to wrap for the entire reports instead of doing for the every line?
Can I do it for the whole document?
I have solutions for line wrap, my problem is that I have many conditions like below:
Firstname lastname route (condition1 ) (condition2) (condition3)
(condition4)..go on...
Let us assume:
First name fixedwidth is 15,
lastname fixed width is 15,city fixed width is 3...
after that condition1 will have 10 width ,condition2 have 15 fixed with then go on...
importantly these conditions are option only...
So 15+emptyspace+15+emptyspace+3 =36 My condition will start from 36 th column..
After the first wrap I got to continue from the same line for the upcoming conditions.
So for the next item i got find the start and end locations.
How to solve this problem ?
xml input:
<?xml version="1.0" encoding="UTF-8"?>
<passengerlist>
<passengers>
<Firstname>JOHNNNNNNNNNNNN</Firstname>
<lastname>MARKKKKKKKKKKKK</lastname>
<comments>abcdefh abc abcde abc dekf jl</comments>
<route>air</route>
</passengers>
<!-- <passengers>
<Firstname>ANTONYYYYYYYYYYY</Firstname>
<lastname>NORMAN</lastname>
<comments>abcdefddddddddghhhhhhhhhhhhhh</comments>
<route>air</route>
</passengers>
<passengers>
<Firstname>BRITTOOOOOOOOOO</Firstname>
<lastname>MARKKKKKKK</lastname>
<comments>abcdedfffghghghghghghghghghghghghgh</comments>
<route>cruise</route>
</passengers> -->
</passengerlist>
XSLT Code:
<!-- For line Wrapping -->
<xsl:template name="callEmpty">
<xsl:param name="callEmpty"/>
<xsl:variable name="LNemptyCheck" select="$callEmpty"></xsl:variable>
</xsl:template>
<xsl:template name="text_wrapper">
<xsl:param name="Text"/>
<xsl:choose>
<xsl:when test="string-length($Text)">
<xsl:value-of select="substring($Text,1,15)"/>
<xsl:if test="string-length($Text) > 15">
<xsl:value-of select="$newline"/>
</xsl:if>
<xsl:call-template name="wrapper_helper">
<xsl:with-param name="Text" select="substring($Text,16)"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="wrapper_helper">
<xsl:param name="Text"/>
<xsl:value-of select="substring($Text,1,15)"/>
<xsl:text>
</xsl:text>
<xsl:call-template name="text_wrapper">
<xsl:with-param name="Text" select="substring($Text,15)"/>
</xsl:call-template>
</xsl:template>
<!-- Template for Line wrapping -->
<xsl:template match="/">
<xsl:for-each select="passengerlist/passengers">
<xsl:value-of select="Firstname"/>
<xsl:text> </xsl:text>
<xsl:value-of select="lastname"/>
<xsl:text> </xsl:text>
<xsl:value-of select="route"/>
<xsl:text> </xsl:text>
<xsl:variable name="firstwrap">
<xsl:if test="route='air'">
<xsl:value-of select="Firstname"/>
<xsl:text> </xsl:text>
<xsl:value-of select="comments"/>
</xsl:if>
</xsl:variable>
<xsl:call-template name="text_wrapper">
<xsl:with-param name="Text" select="$firstwrap"/>
</xsl:call-template>
Output:
JOHNNNNNNNNNNNN MARKKKKKKKKKKKK air JOHNNNNNNNNNNNN
abcdefh abc ab
bcde abc dekf jl
MARKKKKKKKKKKKK abcdefh abc ab bcde abc dekf jl
Expected out:
JOHNNNNNNNNNNNN MARKKKKKKKKKKKK air JOHNNNNNNNNNNNN abcdefh abc ab
bcde abc dekf jl MARKKKKKKKKKKKK abcdefh abc abbcde abc dekf jl
Please help me to sort out my problem or tell me Is it possible in XSLT?
I'm not sure what exactly your problem is (I cannot see any significant difference between output you got and output you expected). But I think it is possible make it simpler. I prepared some testing input xml (just very simple for demonstration).
<?xml version="1.0" encoding="UTF-8"?>
<Input>
<Line>Some long text is on the first line.</Line>
<Line>Some longer text is on the second line.</Line>
<Line>But the longest text occures on the third line.</Line>
</Input>
In following xslt I store the result of processing of each line (i.e. copy of its text and append additional text based on some conditions) into a variable. Then I wrap this variable at once using a user function (it could be done with named template as well).
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:my="my-ns">
<xsl:output method="text" />
<xsl:variable name="newLineCharacter" select="'
'" />
<xsl:variable name="maxLineWidth" select="50" />
<xsl:template match="/">
<xsl:apply-templates select="Input/Line" />
</xsl:template>
<xsl:template match="Line">
<!-- Process the line and store the result into variable-->
<xsl:variable name="processedText">
<xsl:value-of select="." />
<xsl:text> </xsl:text>
<xsl:if test="position() >= 1">
<xsl:text>First condition is true. </xsl:text>
</xsl:if>
<xsl:if test="position() >= 2">
<xsl:text>Second condition is true. </xsl:text>
</xsl:if>
<xsl:if test="position() >= 3">
<xsl:text>Third condition is true. </xsl:text>
</xsl:if>
<!-- et cetera, et cetera ...-->
</xsl:variable>
<!-- Wrap the text stored in a variable -->
<xsl:value-of select="my:wrapText($processedText, $maxLineWidth)" />
</xsl:template>
<xsl:function name="my:wrapText">
<xsl:param name="textToBeWrapped" />
<xsl:param name="maximumWidth" />
<xsl:value-of select="substring($textToBeWrapped,1,$maximumWidth)" />
<xsl:value-of select="$newLineCharacter" />
<xsl:if test="string-length($textToBeWrapped) > $maximumWidth">
<!-- Recursive call of my:wrapText to wrap the rest of the text -->
<xsl:value-of select="my:wrapText(substring($textToBeWrapped,$maximumWidth+1), $maximumWidth)" />
</xsl:if>
</xsl:function>
</xsl:stylesheet>
And the output is
Some long text is on the first line. First conditi
on is true.
Some longer text is on the second line. First cond
ition is true. Second condition is true.
But the longest text occures on the third line. Fi
rst condition is true. Second condition is true. T
hird condition is true.
I hope it will meet your needs.
I use the XSLT 2.0 element analyze-string in a stylesheet that transforms XML to HTML; specifically, I use it to convert string encoding for subscripts in chemical formulae to HTML subscripts. Therefore, the result is a string, to go in a p or td element, with embedded mark-up.
The transformation is supposed to produce output like H2O but in fact inserts a line-break in the HTML:
H
<sub>2</sub>O
and this break is (correctly) interpreted by the browser as a space:
H
2O
which is ugly.
Is there a way to remove the line-break? I've tried putting the whole analyze-string element on one line and that doesn't work.
The input would be something like
<OrdinaryStructralFormula>H$_2$O</OrdinaryStructuralFormula>
for a simple case and
<OrdinaryStructralFormula>C$_2$OH$_5$$^-</OrdinaryStructuralFormula>
for a more-complicated one. Note that the subscript pattern can match multiple times in the general case and can be either in the middle or at the end of the string. The pattern also has to match and eliminate any notation for charge: the $^- bit at the end of the second example.
The XSLT processor is Saxon 9.4 and the XSLT template follows.
<xsl:template name="formula">
<xsl:param name="formula"/>
<xsl:if test="$formula">
<xsl:variable name="f" select="translate($formula, '$', '')"/>
<xsl:analyze-string select="$f" regex="(_)(\d+)|(\^)\d*\+|(\^)\d*\-">
<xsl:matching-substring>
<xsl:if test="regex-group(1)='_'">
<sub><xsl:value-of select="regex-group(2)"/></sub>
</xsl:if>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:if>
</xsl:template>
I cannot reproduce the reported result.
This transformation (which is what you should have given us, but you only provided a template):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="formula">
<xsl:with-param name="formula" select="/*"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="formula">
<xsl:param name="formula"/>
<xsl:if test="$formula">
<xsl:variable name="f" select="translate($formula, '$', '')"/>
<xsl:analyze-string select="$f" regex="(_)(\d+)|(\^)\d*\+|(\^)\d*\-">
<xsl:matching-substring>
<xsl:if test="regex-group(1)='_'">
<sub><xsl:value-of select="regex-group(2)"/></sub>
</xsl:if>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document with Saxon 9.1.05:
<formula>H$_2$O</formula>
produces the wanted, correct result:
H<sub>2</sub>O
When the same transformation is applied on the second XML document:
<OrdinaryStructuralFormula>C$_2$OH$_5$$^-</OrdinaryStructuralFormula>
Again the wanted correct result is produced:
C<sub>2</sub>OH<sub>5</sub>
Do note: I ran the same transformations with two other XSLT 2.0 processors: XQSharp (XMLPrime) and AltovaXML (XML-SPY) and got exactly the same, correct results.
I have two source files which I need to combine on a row by row basis. I am happy reading the files into a variable and I am happy with the logic but the syntax has me stumped. For each row in file 1 I need to loop round each row in file 2 and output the two variables concatenated together:
File 1:
<rows>
<row>1</row>
<row>2</row>
<row>3</row>
<row>4</row>
</rows>
File 2:
<rows>
<row>a</row>
<row>b</row>
</rows>
Required output:
<rows>
<row>1/a</row>
<row>1/b</row>
<row>2/a</row>
<row>2/b</row>
<row>3/a</row>
<row>3/b</row>
<row>4/a</row>
<row>4/b</row>
<rows>
My (poor) attempt at getting the XSLT to work:
<rows>
<xsl:apply-templates select="document('file1.xml')/rows/row" />
</rows>
<xsl:template match="row">
<xsl:apply-templates select="document('file2.xml')/rows/row" />
</xsl:template>
<xsl:template match="row">
<row><xsl:value-of select="???" />/<xsl:value-of select="???" /></row>
</xsl:template>
(These files are simplified versions of what I actually have)
How do I make one template match one 'row' value and the other match another (both source files use the same structure). And how do I set those '???' values?
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vDoc2">
<rows>
<row>a</row>
<row>b</row>
</rows>
</xsl:variable>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<rows>
<xsl:apply-templates/>
</rows>
</xsl:template>
<xsl:template match="row">
<xsl:apply-templates select="$vDoc2/*/row" mode="doc2">
<xsl:with-param name="pValue" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="row" mode="doc2">
<xsl:param name="pValue" />
<row><xsl:sequence select="concat($pValue, '/', .)"/></row>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided first XML document:
<rows>
<row>1</row>
<row>2</row>
<row>3</row>
<row>4</row>
</rows>
the wanted, correct result is produced:
<rows>
<row>1/a</row>
<row>1/b</row>
<row>2/a</row>
<row>2/b</row>
<row>3/a</row>
<row>3/b</row>
<row>4/a</row>
<row>4/b</row>
</rows>
I am still a beginner with XSLT but I am having a difficult task in hand.
I have a non-xml file which needs to be transformed. The format of the file is a s follows:
type1
type1line1
type1line2
type1line3
type2
type2line1
type2line2
type3
type3line1
type3line2
types (type1, type2, ...) are specified using certain codes which don't have a specific order. Each type has multiple line underneath.
So, I need to transform this file but the problem is that for each type I have to do a different transformation for each of it's underlying lines.
Now, I can read the string line by line and determine that a new type has begun but I don't know how to set a flag (indicating the type) to use it in the underlying lines.
Here is what I have right now:
<?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" version="2.0">
<xsl:param name="testString" as="xs:string">
type1
line1
line2
type1
line1
</xsl:param>
<xsl:template match="/">
<xsl:call-template name="main">
<xsl:with-param name="testString" select="$testString"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="main">
<xsl:param name="testString"/>
<xsl:variable name="iniFile" select="$testString"/>
<config>
<xsl:analyze-string select="$iniFile" regex="\n">
<xsl:non-matching-substring>
<item>
<xsl:choose>
<xsl:when test="starts-with(., 'type1')">
<!-- do a specific transformation-->
</xsl:when>
<xsl:when test="starts-with(., 'type2')">
<!-- do another transformation-->
</xsl:when>
</xsl:choose>
</item>
</xsl:non-matching-substring>
</xsl:analyze-string>
</config>
</xsl:template>
</xsl:stylesheet>
Any idea about how to solve the problem.
I think XSLT 2.1 will allow you to use its powerful stuff like for-each-group on sequences of atomic values like strings but with XSLT 2.0 you have such powerful features only for sequences of nodes so my first step when using XSLT 2.0 with plain string data I want to process/group is to create elements. So you could tokenize your data, wrap each token into some element and then use for-each-group group-starting-with to process each group starting with some pattern like '^type[0-9]+$'.
You haven't really told us what you want to with the data once you have identified a group so take the following as an example you could adapt:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="input" as="xs:string">type1
type1line1
type1line2
type1line3
type2
type2line1
type2line2
type3
type3line1
type3line2</xsl:param>
<xsl:template name="main">
<xsl:variable name="lines" as="element(item)*">
<xsl:for-each select="tokenize($input, '\n')">
<item><xsl:value-of select="."/></item>
</xsl:for-each>
</xsl:variable>
<xsl:for-each-group select="$lines" group-starting-with="item[matches(., '^type[0-9]+$')]">
<xsl:choose>
<xsl:when test=". = 'type1'">
<xsl:apply-templates select="current-group() except ." mode="m1"/>
</xsl:when>
<xsl:when test=". = 'type2'">
<xsl:apply-templates select="current-group() except ." mode="m2"/>
</xsl:when>
<xsl:when test=". = 'type3'">
<xsl:apply-templates select="current-group() except ." mode="m3"/>
</xsl:when>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="item" mode="m1">
<foo>
<xsl:value-of select="."/>
</foo>
</xsl:template>
<xsl:template match="item" mode="m2">
<bar>
<xsl:value-of select="."/>
</bar>
</xsl:template>
<xsl:template match="item" mode="m3">
<baz>
<xsl:value-of select="."/>
</baz>
</xsl:template>
</xsl:stylesheet>
When applied with Saxon 9 (command line options -it:main -xsl:sheet.xsl) the result is
<foo>type1line1</foo>
<foo>type1line2</foo>
<foo>type1line3</foo>
<bar>type2line1</bar>
<bar>type2line2</bar>
<baz>type3line1</baz>
<baz>type3line2</baz>