XPTY0004: An empty sequence is not allowed as the value of variable while loading xslt using saxon templatefactory - xslt-1.0

I updated the provided xslt to accept a param "multiplexpaths" from my source and assignin enter code here`g this to nodes variable in xslt to below:
<?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="#all"
version="3.0">
<xsl:param name="multiplexpaths" as="xs:string" static="yes" />
<!-- xsl:param name="copy" as="xs:string" static="yes" select="'//other[. = 1345], //more[. = 2]'"/-->
<xsl:variable name="nodes" _select="{$multiplexpaths}"/>
<xsl:variable name="ancestors" select="$nodes/ancestor::*"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="$nodes">
<xsl:sequence select="."/>
</xsl:template>
<xsl:template match="$ancestors">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

For the xsl:evaluate example, you would need to use Saxon 10 HE, 9.9 HE doesn't support xsl:evaluate.
As for setting a static parameter with Saxon 9.9 and s9api, a simple example is
String paths = "catalog/cd1,catalog/Test/value/a1";
Processor processor = new Processor(true);
XsltCompiler xsltCompiler = processor.newXsltCompiler();
xsltCompiler.setParameter(new QName("copy"), new XdmAtomicValue(paths));
XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource("static-param-example2.xsl"));
Xslt30Transformer xslt30Transformer = xsltExecutable.load30();
xslt30Transformer.transform(new StreamSource("input-sample1.xml"), xslt30Transformer.newSerializer(System.out));
with the input-sample1.xml being
<catalog>
<cd1>
<name>abc</name>
<Stext>1234</Stext>
<Tag>uuuu</Tag>
</cd1>
<cd2>
<name>abc</name>
<Stext>1234</Stext>
<Tag>uuuu</Tag>
</cd2>
<Test>
<value>
<a1>123</a1>
<b1>77474</b1>
</value>
</Test>
</catalog>
and the XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="copy" as="xs:string" static="yes" select="'()'"/>
<xsl:variable name="nodes" _select="{$copy}"/>
<xsl:variable name="ancestors" select="$nodes/ancestor::*"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="$nodes">
<xsl:sequence select="."/>
</xsl:template>
<xsl:template match="$ancestors">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
this outputs
<catalog>
<cd1>
<name>abc</name>
<Stext>1234</Stext>
<Tag>uuuu</Tag>
</cd1>
<Test>
<value>
<a1>123</a1>
</value>
</Test>
</catalog>
So the main point is to set the parameter on the XsltCompiler and not after compilation on the transformer.

Related

ReturnTrue if node is found

I need to form new element if specific node is found, but if that specific node is present several times i need to form only one element, My code is forming the aifound element twice.
<root>
<ai>
<i></i>
</ai>
<ai>
<i></i>
</ai>
</root>
output xml
<root>
<ai>
<i></i>
</ai>
<ai>
<i></i>
</ai>
<aifound>True</aifound>
</root>
My xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ai">
<aifound>True</aifound>
</xsl:template>
</xsl:stylesheet>
Not clear whether you want to add aifound if there are no ais at all. The following xslt adds aifound only if ais exist.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<xsl:if test="//ai">
<aifound>True</aifound>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
How about simply:
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="*"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="*"/>
<aifound>
<xsl:value-of select="boolean(ai)"/>
</aifound>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Remove Namespace on Element

I want to remove namespace at output structure. I prepared XSLT code
but it gives namespace on this element
My Input XML is this.
<?xml version='1.0' encoding='UTF-8'?>
<n0:Messages xmlns:n0="http://sap.com/xi/XI">
<n0:Message>
<ContactData>
<Data>
<information>
<Name>A</Name>
<Phone>123456</Phone>
</information>
</Data>
</ContactData>
</n0:Message>
</n0:Messages>
XSLT CODE implemented
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n0="http://sap.com/xi/XI" exclude-result-prefixes="n0">
<!-- Output -->
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select= "//ContactData"/>
</xsl:template>
<xsl:template match="//*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Present output:
<?xml version='1.0' encoding='UTF-8'?>
<ContactData xmlns:n0="http://sap.com/xi/XI">
<Data>
<information>
<Name>A</Name>
<Phone>123456</Phone>
</information>
</Data>
</ContactData>
Output expected
<?xml version='1.0' encoding='UTF-8'?>
<ContactData>
<Data>
<information>
<Name>A</Name>
<Phone>123456</Phone>
</information>
</Data>
</ContactData>
Please help on this code
Thank you very much.
If you're able to use XSLT 2.0, you can achieve the required output simply by:
<xsl:stylesheet version="2.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="*"/>
<xsl:template match="/">
<xsl:copy-of select="*/*/*" copy-namespaces="no"/>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/3NSSEuK
In XSLT 1.0, it takes a bit more work:
<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="*"/>
<xsl:template match="/">
<xsl:apply-templates select="*/*/*" />
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/3NSSEuK/1
Try this by using template * and # :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:n0="http://sap.com/xi/XI"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs math n0" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/*:Messages/ContactData"/>
</xsl:template>
</xsl:stylesheet>

template not getting applied with XSLT

I am using xslt to convert xml to xml.
<root>
<elem>
<confs>
<conf1>1</conf1>
<conf2>2</conf2>
</confs>
</elem>
</root>
My XSL
<xsl:template match="elem">
<xsl:copy>
<xsl:attribute name="className">confs</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="confs">
<confs>
<xsl:for-each select="*">
<conf>
<value>
<xsl:value-of select="node()"></xsl:value-of>
</value>
</conf>
</confs>
</xsl:template>
desired output:
<root>
<elem className="confs>
<confs>
<conf>
<value>1</value>
</conf>
<conf>
<value>1</value>
</conf>
</confs>
</elem>
</root>
When ran each template individaully they are good. But I run both the confs template is not affected at all.
Any help?
I believe the most straightforward way to achieve your output would be by:
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="elem">
<elem className="confs">
<xsl:apply-templates/>
</elem>
</xsl:template>
<xsl:template match="confs/*">
<conf>
<value>
<xsl:value-of select="."/>
</value>
</conf>
</xsl:template>
</xsl:stylesheet>

Custom XSLT Tag

I am sick of write all the call-template/with-param stuff.
Is there any shortcut for this in example:
<xsl:call-template name="complexwidget">
<xsl:with-param name="a" select="$bla_213"/>
<xsl:with-param name="b" select="$bla_213"/>
<xsl:with-param name="c" select="$bla_213"/>
<xsl:with-param name="d" select="$bla_213"/>
</xsl:call-template>
A Perfect posibility would be this:
<complex a="{$bla_213}" b="{$bla_213}" c="{$bla_213}" d="{$bla_213}" />
Any idea, maybe a twice-transform-xslt??
If you could switch to xslt 2.0 then user functions would be exactly what you need.
<?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-namespace">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:value-of select="my:complexWidget(1, 2, 3, 4)" />
</xsl:template>
<xsl:function name="my:complexWidget">
<xsl:param name="a" />
<xsl:param name="b" />
<xsl:param name="c" />
<xsl:param name="d" />
<!-- Do something -->
<xsl:value-of select="($a, $b,$c, $d)" separator="-" />
</xsl:function>
</xsl:stylesheet>
In xslt 1.0 I think that preprocessing of xslt stylesheet with another transformation will be the only way (or may be writing some extension functions in language of your processor .
EDIT:
I tried the "preprocess" of xslt and it seems to be working (but probably it is no the most elegant way how to do it).
Input xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mypp="my-preprocess-namespace">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:variable name="varA" select="1"/>
<xsl:variable name="varB" select="2"/>
<xsl:variable name="varC" select="3"/>
<xsl:variable name="varD" select="4"/>
<mypp:complexWidget a="$varA" b="$varB" c="$varC" d="$varD"/>
</xsl:template>
<xsl:template name="complexWidget">
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:param name="c"/>
<xsl:param name="d"/>
<!-- do something-->
<xsl:value-of select="$a"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$b"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$c"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$d"/>
</xsl:template>
</xsl:stylesheet>
I defined here a special namespace for element to be replaced (i.e. xmlns:mypp="my-preprocess-namespace"). Element to be replaced is <mypp:complexWidget a="$varA" b="$varB" c="$varC" d="$varD"/>.
This xslt I preprocess with following xslt.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mypp="my-preprocess-namespace">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<xsl:template match="mypp:*">
<xsl:element name="xsl:call-template">
<xsl:attribute name="name">
<xsl:value-of select="local-name()" />
</xsl:attribute>
<xsl:for-each select="#*">
<xsl:element name="xsl:with-param">
<xsl:attribute name="name">
<xsl:value-of select="name()" />
</xsl:attribute>
<xsl:attribute name="select">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
It is based on "Identity transform" - copy everything to the output just elements from special namespace transform into the call-template element. I guess this could be done in more nice way than I did.
The output is
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:mypp="my-preprocess-namespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:variable name="varA" select="1"/>
<xsl:variable name="varB" select="2"/>
<xsl:variable name="varC" select="3"/>
<xsl:variable name="varD" select="4"/>
<xsl:call-template name="complexWidget">
<xsl:with-param name="a" select="$varA"/>
<xsl:with-param name="b" select="$varB"/>
<xsl:with-param name="c" select="$varC"/>
<xsl:with-param name="d" select="$varD"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="complexWidget">
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:param name="c"/>
<xsl:param name="d"/>
<!-- do something-->
<xsl:value-of select="$a"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$b"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$c"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="$d"/>
</xsl:template>
</xsl:stylesheet>
When I run the output as xslt stylesheet it produces expected output.
But I don't have any deeper experience with producing xslt stylesheet by another xslt so I don't know much about possible troubles with this approach.

XSL 1.0 - Hiding Child nodes when child element is Null

I have been trying to make child elements of my structure hide based upon them being empty.
From reading other posts I found this Remove parent node if a child node is empty but I don't understand it enough to implement it in my XSL. I have had a try at applying the linked post to my XSL but it does not make the desired changes to my output.
So my XML is like this:
<?xml version="1.0" encoding="UTF-8" ?>
<exchange>
<sce>
<sce.srs>
<sce_scjc.sce.srs>140008305/1</sce_scjc.sce.srs>
<sce_seq2.sce.srs>01</sce_seq2.sce.srs>
<sce_stuc.sce.srs>140008305</sce_stuc.sce.srs>
<spr>
<spr.cams>
<spr_code.spr.cams>140008305/1</spr_code.spr.cams>
<prs_code.spr.cams>77711925</prs_code.spr.cams>
<prs>
<prs.mensys>
<prs_code.prs.mensys>77711925</prs_code.prs.mensys>
<prs_name.prs.mensys>Johan</prs_name.prs.mensys>
</prs.mensys>
</prs>
</spr.cams>
</spr>
</sce.srs>
<sce.srs>
<sce_scjc.sce.srs>151516736/1</sce_scjc.sce.srs>
<sce_seq2.sce.srs>01</sce_seq2.sce.srs>
<sce_stuc.sce.srs>151516736</sce_stuc.sce.srs>
<spr>
<spr.cams>
<spr_code.spr.cams>151516736/1</spr_code.spr.cams>
<prs_code.spr.cams>77709062</prs_code.spr.cams>
<prs>
<prs.mensys>
<prs_code.prs.mensys>77709062</prs_code.prs.mensys>
<prs_name.prs.mensys>Evangelia</prs_name.prs.mensys>
</prs.mensys>
</prs>
</spr.cams>
</spr>
</sce.srs>
<sce.srs>
<sce_scjc.sce.srs>150052468/1</sce_scjc.sce.srs>
<sce_seq2.sce.srs>01</sce_seq2.sce.srs>
<sce_stuc.sce.srs>150052468</sce_stuc.sce.srs>
<spr>
<spr.cams>
<spr_code.spr.cams>150052468/1</spr_code.spr.cams>
<prs_code.spr.cams/>
</spr.cams>
</spr>
</sce.srs>
</sce>
</exchange>
And my XSL looks like the passaage below. I have a nil element template that I added in as I thought looking for a value was easier than looking for a nulll so happy for it to come out if not needed.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" exclude-result-prefixes="xd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/exchange/sce">
<ImportTask>
<EntityRelationshipEntities>
<xsl:apply-templates select="node()|#*"/>
</EntityRelationshipEntities>
</ImportTask>
</xsl:template>
<xsl:template name="nilElement">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="string($value)">
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="xsi:nil" namespace="http://www.w3.org/2001/XMLSchema-instance">True</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="sce.srs[not(*/AttributeValue[not(#AttributeValue='True')])]">
<xsl:for-each select="spr/spr.cams">
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode><xsl:value-of select="../../sce_stuc.sce.srs"/></EntityCode>
<AttributeValue>
<xsl:call-template name="nilElement">
<xsl:with-param name="value" select="prs/prs.mensys/prs_name.prs.mensys"/>
</xsl:call-template>
</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</xsl:for-each>
</xsl:template>
<xsl:template match="AttributeValue[#AttributeValue = 'True']"/>
</xsl:stylesheet>
So this currently gives me:
<ImportTask xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EntityRelationshipEntities>
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>151514490</EntityCode>
<AttributeValue xsi:nil="True"/>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>140008305</EntityCode>
<AttributeValue>Johan</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
<EntityRelationshipEntity>
<EntityCode>151516736</EntityCode>
<AttributeValue>Evangelia</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</EntityRelationshipEntities>
</ImportTask>
What I would like to produce is out those where is not null. This would mean that the child below is not output:
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>151514490</EntityCode>
<AttributeValue xsi:nil="True"/>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
But the others would be output something like this:
<ImportTask xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EntityRelationshipEntities>
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>140008305</EntityCode>
<AttributeValue>Johan</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
<EntityRelationshipEntity>
<EntityCode>151516736</EntityCode>
<AttributeValue>Evangelia</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</EntityRelationshipEntities>
</ImportTask>
Can someone help me apply this correctly?
Many thanks
Jonah
Perhaps I am missing something, but couldn't this be simply:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/exchange/sce">
<ImportTask xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EntityRelationshipEntities>
<xsl:apply-templates select="sce.srs/spr/spr.cams[string(prs/prs.mensys/prs_name.prs.mensys)]"/>
</EntityRelationshipEntities>
</ImportTask>
</xsl:template>
<xsl:template match="spr.cams">
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>
<xsl:value-of select="../../sce_stuc.sce.srs"/>
</EntityCode>
<AttributeValue>
<xsl:value-of select="prs/prs.mensys/prs_name.prs.mensys"/>
</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</xsl:template>
</xsl:stylesheet>