XSLT select text without children - xslt

XML:
<data><ph>Foo</ph>Bar</data>
XSL:
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="data/ph"/>
<xsl:apply-templates select="data"/>
</xsl:template>
<xsl:template match="data/ph">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="data">
<xsl:value-of select="."/>
</xsl:template>
When the XSL selects the text in the /data/ with <xsl:template match="data"><xsl:value-of select="."/> it is also selecting the text in the child entity data/ph. How do I point to only the text of /data/, without including the text of /data/ph/ ?
My output should be: FooBar, and not FooFooBar.

When the XSL selects the text in the /data/ with <xsl:template
match="data"><xsl:value-of select="."/> it is also selecting the text
in the child entity data/ph. How do I point to only the text of
/data/, without including the text of /data/ph/ ?
Use:
<xsl:copy-of select="text()"/>
This copies all text node-children of the current node.
With this correction, the whole transformation becomes:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="data/ph"/>
<xsl:apply-templates select="data"/>
</xsl:template>
<xsl:template match="data/ph">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="data">
<xsl:copy-of select="text()"/>
</xsl:template>
</xsl:stylesheet>
and when applied on the provided XML document:
<data><ph>Foo</ph>Bar</data>
the wanted, correct result is produced:
FooBar

Related

Conditionally modifying text in multiple identically named children

I'm trying to conditionally modify the content of some XML. Am element has multiple identically named children, which I want to modify based on the text content. For example, I have the below XML:
<first>
<second>
<third>alice</third>
<third>bob</third>
<third>charlie</third>
</second>
</first>
Which I'd like to transform into:
<first>
<second>
<third>xavier</third>
<third>yvonne</third>
<third>charlie</third>
</second>
</first>
I had thought the below xsl would work, but it doesn't (I suspect for a couple of reasons). What am I doing wrong?
<?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" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/first/second/third/">
<xsl:choose>
<xsl:when test="contains(text(), 'alice')">
<xsl:text>xavier</xsl:text>
</xsl:when>
<xsl:when test="contains(text(), 'bob')">
<xsl:text>Yvonne</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Two things you're doing wrong:
You have a slash at the end of this XPath /first/second/third/. Syntactically, it's not legal to have a slash at the end of an XPath, and you don't need one here.
You have a template that's supposed to match third elements (basically providing a substitute for what they once were), but you're not copying the elements. You're just replacing them with text, which means you'd have a result like:
<first>
<second>xavieryvonnecharlie</second>
</first>
In order to get your attempt to work, modifying the template to this should be sufficient:
<xsl:template match="/first/second/third">
<xsl:copy>
<xsl:choose>
<xsl:when test="contains(text(), 'alice')">
<xsl:text>xavier</xsl:text>
</xsl:when>
<xsl:when test="contains(text(), 'bob')">
<xsl:text>Yvonne</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
However can do this much more cleanly by having templates to match the text nodes you want to replace:
<?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" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="third/text()[. = 'alice']">xavier</xsl:template>
<xsl:template match="third/text()[. = 'bob']">yvonne</xsl:template>
</xsl:stylesheet>
When run on your sample input, the result is:
<first>
<second>
<third>xavier</third>
<third>yvonne</third>
<third>charlie</third>
</second>
</first>

Avoid repetition in XSLT template rules using XSLT 1.0

Given the following XML document:
<document>
<a>123</a>
<foo_1>true</foo_1>
<foo_2>false</foo_2>
<foo_3>true</foo_3>
<foo_4>true</foo_4>
<foo_5>false</foo_5>
<b/>
<bar_1>false</bar_1>
<bar_2>false</bar_2>
<bar_3>true</bar_3>
<bar_4>false</bar_4>
<bar_5>true</bar_5>
<c>some text</c>
</document>
I want to transform this document by eliminating all enumerated elements containing false and by converting all enumerated elements containing true into the form <prefix_n>value</prefix_n>, where value is the number after the underscore.
For the example given above the result should look like this:
<document>
<a>123</a>
<foo_n>1</foo_n>
<foo_n>3</foo_n>
<foo_n>4</foo_n>
<b/>
<bar_n>3</bar_n>
<bar_n>5</bar_n>
<c>some text</c>
</document>
For this I am using the following transformation, which works fine.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<!-- standard copy template -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[substring(name(), 1, 4) = 'foo_']">
<xsl:if test=". = 1 or . = 'true'">
<xsl:element name="foo_n">
<xsl:value-of select="substring-after(name(), 'foo_')"/>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="*[substring(name(), 1, 4) = 'bar_']">
<xsl:if test=". = 1 or . = 'true'">
<xsl:element name="bar_n">
<xsl:value-of select="substring-after(name(), 'bar_')"/>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Now I'm facing the problem that I have to deal with a multitude of different prefixes, not just foo_ and bar_.
Is there a way to turn the template rules into a named template where I can pass in the prefix as an argument, thereby avoiding to write a lot of repetitive template rules?
Here's one way you could look at it:
XSLT 1.0
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'foo_') or starts-with(name(), 'bar_')]">
<xsl:if test=". = 1 or . = 'true'">
<xsl:element name="{substring-before(name(), '_')}-n">
<xsl:value-of select="substring-after(name(), '_')"/>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Here's another:
XSLT 1.0
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[contains(translate(name(), '123456789', '000000000'), '_0')]">
<xsl:if test=". = 1 or . = 'true'">
<xsl:variable name="prefix" select="substring-before(translate(name(), '123456789', '000000000'), '_0')" />
<xsl:element name="{$prefix}-n">
<xsl:value-of select="substring(name(), string-length($prefix) + 2)"/>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This last one should work with any prefix followed by _[1-9] - including prefixes that contain an additional underscore and including prefixes that contain other prefixes.
Is there a way to turn the template rules into a named template where I can pass in the prefix as an argument
Well, sure:
<xsl:template name="if-true">
<xsl:param name="prefix"/>
<xsl:if test=". = 1 or . = 'true'">
<xsl:element name="{concat($prefix, 'n')}">
<xsl:value-of select="substring-after(name(), $prefix)"/>
</xsl:element>
</xsl:if>
</xsl:template>
<!-- example using the above: -->
<xsl:template match="*[starts-with(name(), 'foo_')]">
<xsl:call-template name="if-true">
<xsl:with-param name="prefix" select="'foo_'"/>
</xsl:call-template>
</xsl:template>
But it's not clear whether that would really achieve your objective:
, thereby avoiding to write a lot of repetitive template rules?
because you would still need a bunch of templates of the second kind to match the specific elements you want to transform. A solution along the lines #michael.hor257k suggested, that avoids duplication by making the same template match all the elements you want to transform, is a better alternative.

correct xsl template matching syntax

I'm learning XSL and hope to get some help. I want to extract part of the following datasets.xml and output them as tab delimited texts:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<listDatasetsResponse xmlns="http://www.algorithmics.com/schema">
<status>OK</status>
<datasets size="31">
<dataset>
<id>stress_20150910_20150910_160259</id>
<basedOn>Mimm_20150910_20150910_030922</basedOn>
<active>false</active>
<sandbox>true</sandbox>
<ownedBy>admin</ownedBy>
<createdOn>2015-09-10T16:04:24.199-04:00</createdOn>
<createdBy>rtcesupp</createdBy>
<evaluated>true</evaluated>
<stopped>false</stopped>
</dataset>
<dataset>
<id>imm_20150910_20150910_140315</id>
<basedOn>Mimm_20150910_20150910_030922</basedOn>
<active>true</active>
<sandbox>true</sandbox>
<ownedBy>admin</ownedBy>
<createdOn>2015-09-10T14:04:42.696-04:00</createdOn>
<createdBy>rtcesupp</createdBy>
<evaluated>true</evaluated>
<stopped>false</stopped>
</dataset>
</datasets>
</listDatasetsResponse>
here is the XSL I used:
$ vi dataset.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:strip-space elements="*" />
<xsl:template match="/listDatasetsResponse/datasets/dataset">
<xsl:value-of select="id"/><xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test="active='true'">
<xsl:text>ACTIVE</xsl:text>
</xsl:when>
<xsl:when test="stopped='true'">
<xsl:text>,STOPPED</xsl:text>
</xsl:when>
<xsl:when test="evaluated='true'">
<xsl:text>,EVALUATED</xsl:text>
</xsl:when>
</xsl:choose>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
I expected the result of:
stress_20150910_20150910_160259 EVALUATED
imm_20150910_20150910_140315 ACTIVE,EVALUATED
but what I got is:
OKstress_20150910_20150910_160259Mimm_20150910_20150910_030922falsetrueadmin2015-09-10T16:04:24.199-04:00rtcesupptruefalseimm_20150910_20150910_140315Mimm_20150910_20150910_030922truetrueadmin2015-09-10T14:04:42.696-04:00rtcesupptruefalse
It seemed like the XSL stylesheet was ignored. Could some one point me to correct XSL template matching syntax?
The fundamental problem in your XSL is that none of the XPath expression being used match the element in the source XML. Notice your XML has default namespace declared at the root element :
xmlns="http://www.algorithmics.com/schema"
Descendant elements without explicit prefix and without local default namespace inherits ancestor default namespace implicitly. To match element in namespace, simply declare a prefix that point to the namespace-uri and use that prefix in your XPath expressions, for example :
<xsl:stylesheet .....
xmlns:d="http://www.algorithmics.com/schema">
.....
<xsl:template match="/d:listDatasetsResponse/d:datasets/d:dataset">
<xsl:value-of select="d:id"/><xsl:text> </xsl:text>
.....
</xsl:template>
</xsl:stylesheet>
How about ...
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.algorithmics.com/schema"
version="1.0" >
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="*" />
<xsl:template match="#*|node()" />
<xsl:template match="/">
<xsl:apply-templates select="a:listDatasetsResponse/a:datasets/a:dataset" />
</xsl:template>
<xsl:template match="a:dataset">
<xsl:value-of select="a:id"/>
<xsl:text> </xsl:text>
<xsl:apply-templates select="a:active|a:stopped|a:evaluated" />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="a:active[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:text>ACTIVE</xsl:text>
</xsl:template>
<xsl:template match="a:stopped[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:text>STOPPED</xsl:text>
</xsl:template>
<xsl:template match="a:evaluated[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:text>EVALUATED</xsl:text>
</xsl:template>
</xsl:stylesheet>
... or this version ...
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.algorithmics.com/schema"
version="1.0" >
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="*" />
<xsl:template match="#*|node()" />
<xsl:template match="/">
<xsl:apply-templates select="a:listDatasetsResponse/a:datasets/a:dataset" />
</xsl:template>
<xsl:template match="a:dataset">
<xsl:value-of select="a:id"/>
<xsl:text> </xsl:text>
<xsl:apply-templates select="a:active|a:stopped|a:evaluated" />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="a:active[.='true'] | a:stopped[.='true'] | a:evaluated[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:value-of select="translate( local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
</xsl:template>
</xsl:stylesheet>

apply two consecutive xslt transformations

I have two xslt transformations to apply to an xml message.
The first is to drop all namespaces and prefixes. here is the code :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()[not(self::*)]">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|#*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The second one is to select elements from the output of the first :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<onSale><xsl:value-of select="//entry/content/product_product/sale_ok" /></onSale>
<onlineOnly><xsl:value-of select="//entry/content/product_product/online" /></onlineOnly>
<name><xsl:value-of select="//entry/content/product_product/name" /></name>
<isbn><xsl:value-of select="//entry/content/product_product/isbn" /></isbn>
<price><xsl:value-of select="//entry/content/product_product/price" /></price>
<active><xsl:value-of select="//entry/content/product_product/active" /></active>
<format><xsl:value-of select="//entry/content/product_product/format" /></format>
<collection><xsl:value-of select="//entry/content/product_product/collection" /></collection>
<dateParution><xsl:value-of select="//entry/content/product_product/date_parution"/></dateParution>
<ean13><xsl:value-of select="//entry/content/product_product/ean13"/></ean13>
</xsl:template>
</xsl:stylesheet>
How I can apply the two of them in one xslt transformation without doing two transformation separately .
Thanks
I don't know which version of XSLT is supported in your environment, with XSLT 2.0 you can use
<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="step1">
<xsl:apply-templates mode="step1"/>
</xsl:variable>
<xsl:template match="#*|node()[not(self::*)]" mode="step1">
<xsl:copy/>
</xsl:template>
<xsl:template match="*" mode="step1">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|#*" mode="step1"/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$step1//entry"/>
</xsl:template>
<xsl:template match="entry">
<onSale><xsl:value-of select="content/product_product/sale_ok" /></onSale>
<onlineOnly><xsl:value-of select="content/product_product/online" /></onlineOnly>
<name><xsl:value-of select="content/product_product/name" /></name>
<isbn><xsl:value-of select="content/product_product/isbn" /></isbn>
<price><xsl:value-of select="content/product_product/price" /></price>
<active><xsl:value-of select="content/product_product/active" /></active>
<format><xsl:value-of select="content/product_product/format" /></format>
<collection><xsl:value-of select="content/product_product/collection" /></collection>
<dateParution><xsl:value-of select="content/product_product/date_parution"/></dateParution>
<ean13><xsl:value-of select="content/product_product/ean13"/></ean13>
</xsl:template>
</xsl:stylesheet>
I would further refactor however and write templates like
<xsl:template match="sale_ok">
<onSale>
<xsl:value-of select="."/>
</onSale>
</xsl:template>
If you use XSLT 1.0 you need an extension function to process the contents of a variable with apply-templates so doing
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="step1">
<xsl:apply-templates mode="step1"/>
</xsl:variable>
<xsl:template match="#*|node()[not(self::*)]" mode="step1">
<xsl:copy/>
</xsl:template>
<xsl:template match="*" mode="step1">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|#*" mode="step1"/>
</xsl:element>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="exsl:node-set($step1)//entry"/>
</xsl:template>
<xsl:template match="entry">
<onSale><xsl:value-of select="content/product_product/sale_ok" /></onSale>
<onlineOnly><xsl:value-of select="content/product_product/online" /></onlineOnly>
<name><xsl:value-of select="content/product_product/name" /></name>
<isbn><xsl:value-of select="content/product_product/isbn" /></isbn>
<price><xsl:value-of select="content/product_product/price" /></price>
<active><xsl:value-of select="content/product_product/active" /></active>
<format><xsl:value-of select="content/product_product/format" /></format>
<collection><xsl:value-of select="content/product_product/collection" /></collection>
<dateParution><xsl:value-of select="content/product_product/date_parution"/></dateParution>
<ean13><xsl:value-of select="content/product_product/ean13"/></ean13>
</xsl:template>
</xsl:stylesheet>
On the other I wonder why we are getting lots of questions recently trying to eliminate namespaces or to ignore them, if you really want to do that with XSLT 2.0 then doing
<xsl:template match="*:sale_ok">
<onSale>
<xsl:value-of select="."/>
<onSale>
</xsl:template>
and so on suffices.

How to set a namespace prefix to a payload xsl

I have this payload, what I am intresting to know is how i can add this "TV:" namespace prefix to all nodes and elementes in it.
<TVInqResponse>
<TVInqRS>
<StatusCode>0</StatusCode>
<StatusDescription>Success</StatusDescription>
This is what is expect to have as a result:
<**tv**:TVInqResponse>
<**tv**:TVInqRS>
<**tv**:StatusCode>0</**tv**:StatusCode>
<**tv**:StatusDescription>Success</**tv**:StatusDescription>
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tv="some:tv">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="tv:{name()}" namespace="some:tv">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{name}"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document (based on the malformed provided "playload"...):
<TVInqResponse>
<TVInqRS>
<StatusCode>0</StatusCode>
<StatusDescription>Success</StatusDescription>
</TVInqRS>
</TVInqResponse>
produces the wanted, correct result:
<tv:TVInqResponse xmlns:tv="some:tv">
<tv:TVInqRS>
<tv:StatusCode>0</tv:StatusCode>
<tv:StatusDescription>Success</tv:StatusDescription>
</tv:TVInqRS>
</tv:TVInqResponse>
If you also want any attribute name to be in the same namespace, replace the template matching attributes with this one:
<xsl:template match="#*">
<xsl:attribute name="tv:{name()}" namespace="some:tv">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>