XSLT conditionally write to two different files - xslt-2.0

I need to extract log meesages from an XML file and write them out to plain text files. The log messages come in two flavors, and I want to write them to separate files.
I have written a style sheet that does exactly what I need except that it sometimes creates empty files because the XML file may not contain messages of one type or another.
I am wondering, 1) if what I ma doing is the best method to do this, and 2) if there is a way to suppress empty files.
My sample may contain errors because it has been retyped. (the original is on a closed network)
Note: I am using XSLT 2.0 features.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" encoding="iso-8859-1" />
<xsl:param name="break" select="string('
')" />
<xs:template match="/">
<xsl:result-document method="text" href="foo.txt">
<xsl:apply-templates select="Root/a/b/c[contains(., 'foo')]" />
</xsl:reult-document>
<xsl:result-document method="text" href="bar.txt">
<xsl:apply-templates select="Root/a/b/c[not(contains(., 'foo'))]" />
</xsl:reult-document>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select=concat(normalize-space(.), $break)" />
</xsl:template>
</xsl:stylesheet>

You could use some XSLT 2.0 stylesheet like:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="break" select="string('
')" />
<xsl:template match="/">
<xsl:apply-templates select="Root/a/b/c"/>
</xsl:template>
<xsl:template match="/Root/a/b/c[contains(., 'foo')]">
<xsl:result-document method="text" href="foo.txt">
<xsl:next-match/>
</xsl:result-document>
</xsl:template>
<xsl:template match="/Root/a/b/c[not(contains(., 'foo'))]">
<xsl:result-document method="text" href="bar.txt">
<xsl:next-match/>
</xsl:result-document>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="concat(normalize-space(.), $break)" />
</xsl:template>
</xsl:stylesheet>
Note: Pattern matching and xsl:next-match.

Related

Error message 'adc' is an undeclared namespace

All, am im relatively new to XSLT but did some solutions allready. Currently i am trying to write an xslt that moves a file to different folders based on if
<adc:Status>uploaded</adc:Status>
is "uploaded" or not.
Despite declasring the xmlns:adc Namespace i get the error
Exception Occured during TransForm using XSLT 2.0 (SaxonHe):
System.Xml.XmlException: 'adc' is an undeclared namespace. Line 2,
position 4.
I spent hours looking for the error messages, trying to getmyself into namespaces, but currently i cant seem to find out whats the cause of the error.
This is the xslt i wrote
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:adc="http://www.ifra.com/adconnexion/#v2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs xsi adc">
<xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="/">
<xsl:variable name="Filename">
<xsl:value-of select="replace(document-uri(.), '.*/', '')"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="/adc:adConnexion/adc:Requests/adc:AdOrder/adc:ProductionDetail/adc:Material/adc:Status='uploaded'">
<!-- Business case -->
<xsl:variable name="OutputFileName" select="concat('file:///C:\UPLOADED\',$Filename)"/>
<xsl:result-document href="{$OutputFileName}">
<xsl:copy-of select="current()"/>
</xsl:result-document>
</xsl:when>
<xsl:otherwise>
<!-- Non Business case -->
<xsl:variable name="OutputFileName" select="concat('file:///C:\ELSE\',$Filename)"/>
<xsl:result-document href="{$OutputFileName}">
<xsl:copy-of select="current()"/>
</xsl:result-document>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The simplified inputfile is
<adc:adConnexion xmlns:adc="http://www.ifra.com/adconnexion/#v2">
<adc:Requests>
<adc:AdOrder messageClass="BusinessTransaction" messageID="FEA*" bookingID="1234" messageCode="AD-O">
<adc:ProductionDetail>
<adc:Material>
<adc:Status>uploaded</adc:Status>
</adc:Material>
</adc:AdOrder>
</adc:Requests>
</adc:adConnexion>
Any help is greatly appreciated!

XSLT: output " without it being parsed

I am trying to achieve the following XML output:
<Foo bar=""" />
My XSLT file is as follows:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:variable name="quote">
<xsl:text>"</xsl:text>
</xsl:variable>
<Foo bar="{$quote}"/>
</xsl:template>
</xsl:stylesheet>
Unfortunately, this gives me the output:
<Foo bar="""/>
How do I alter my XSLT to output & quot; without it being parsed into either a " character or a & #34;?
Ian Roberts has already made the very good point that it doesn't actually matter. But if you really, really wanted to do this, then in XSLT 2.0 (but not XSLT 1.0) you could make use of a character map, like so:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" use-character-maps="quotes" />
<xsl:character-map name="quotes">
<xsl:output-character character=""" string="&quot;" />
</xsl:character-map>
<xsl:template match="/">
<xsl:variable name="quote">
<xsl:text>"</xsl:text>
</xsl:variable>
<Foo bar="{$quote}"/>
</xsl:template>
</xsl:stylesheet>

XSLT set directory where result document ends up

The XSLT below creates result-documents as desired, with one exception: the result document ends up in the directory where the stylesheet was invoked from. I want the result document to be where it was found (i.e. overwrite itself with the transform version).
How can I do that?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xpath-default-namespace="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<xsl:for-each select="collection(iri-to-uri('file:///home/paul/Text/?select=*.xhtml'))">
<xsl:variable name="filename">
<xsl:value-of select="tokenize(document-uri(.), '/')[last()]"/>
</xsl:variable>
<xsl:result-document indent="yes" method="xml" href="{$filename}">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- transform templates removed -->
</xsl:stylesheet>
Try just using href="{document-uri(.)}" to use the full uri as the target rather than doing the tokenize to pull out the last segment.

How do I merge and concatenate the data from each row in two separate source files?

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>

XSLT mapping and summing source children into a single target attribute

I have a source document with XML structure similar to this:
<FOO>
<BAR>x</BAR>
<BAR>y</BAR>
<BAR>z</BAR>
</FOO>
My target XML must have an attribute with a number that represents the numerical sum of x, y and z. Where x = 1, y = 2 and z = 3
NOTE: The x, y and z are not actually numbers in the source document. They are letters and need to be mapped to the numbers that they represent first.
In this case, the target should look something like:
<Target Sum=6>
</Target>
Anyone have an XSLT example that would do what I need?
Thanks in advance
<?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" indent="yes"/>
<xsl:template match="/">
<Target Sum="{sum(FOO/BAR)}" />
</xsl:template>
EDIT:
This is a bit verbose, and there is probably a more elegant way to do it, but essentially I have a named template here that recursively calls itself to calculate the sum after the hard-coded mapping occurs:
<?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" indent="yes"/>
<xsl:template match="/">
<Target>
<xsl:attribute name="Sum">
<xsl:call-template name="GetSum">
<xsl:with-param name="CurrentNode" select="FOO/BAR[position()=1]"/>
</xsl:call-template>
</xsl:attribute>
</Target>
</xsl:template>
<xsl:template name="GetSum">
<xsl:param name="CurrentNode"/>
<xsl:param name="Number" select="0"/>
<xsl:variable name="Recursive_Result">
<xsl:variable name="MappedNumber">
<xsl:choose>
<xsl:when test="$CurrentNode/. = 'x'">1</xsl:when>
<xsl:when test="$CurrentNode/. = 'y'">2</xsl:when>
<xsl:when test="$CurrentNode/. = 'z'">3</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- if there is a next sibling, recursively call GetSum -->
<xsl:choose>
<xsl:when test="$CurrentNode/following-sibling::BAR[1]">
<xsl:call-template name="GetSum">
<xsl:with-param name="CurrentNode" select="$CurrentNode/following-sibling::BAR[1]"/>
<xsl:with-param name="Number">
<xsl:value-of select="$MappedNumber"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$MappedNumber"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- add the recursive_result to the number passed into the template. this will eventually build a sum -->
<xsl:value-of select="$Recursive_Result + $Number"/>
</xsl:template>
</xsl:stylesheet>
A much simpler, shorter and efficient solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kMap" match="#value" use="../#key"/>
<xsl:variable name="vMaps" as="element()*">
<map key="x" value="1"/>
<map key="y" value="2"/>
<map key="z" value="3"/>
</xsl:variable>
<xsl:template match="/*">
<Target Sum="{sum(key('kMap', BAR, document('')))}"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<FOO>
<BAR>x</BAR>
<BAR>y</BAR>
<BAR>z</BAR>
</FOO>
the wanted, correct result is produced:
<Target Sum="6"/>
Explanation: Appropriate use of xsl:key, the 3rd argument of the key() function and AVT.

Resources