Not select a particular node child Elements in xslt using not - xslt

My XML is below. Is it possible to do this in same XSLT?
<response context="XXXX" type="abcd" errorCode="0" >
<output>
<Applicants>
<Applicant>
<IndividualEmployments/>
<Addresses/>
</Applicant>
</Applicants>
<Assets>
<Asset id="12345"></Asset>
</Assets>
<Liabilities>
<Liability id="8765"></Liability>
</Liabilities>
</output>
Desired output should be like below. I want two response nodes, one with Assets and the other with Liabilities.
<response context="XXXX" type="abcd" errorCode="0">
<output>
<Applicants>
<Applicant>
<IndividualEmployments/>
<Addresses/>
</Applicant>
</Applicants>
<Assets>
<Asset id="12345"></Asset>
</Assets>
</output>
<response context="XXXX" type="abcd" errorCode="0">
<output>
<Applicants>
<Applicant>
<IndividualEmployments/>
<Addresses/>
</Applicant>
</Applicants>
<Liabilities>
<Liability id="8765"></Liability>
</Liabilities>
</output>

You need to process the response element and output it twice, making sure the content is different, for instance by passing a parameter:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="response">
<xsl:next-match>
<xsl:with-param name="exclude" tunnel="yes" select="descendant::Liabilities"/>
</xsl:next-match>
<xsl:next-match>
<xsl:with-param name="exclude" tunnel="yes" select="descendant::Assets"/>
</xsl:next-match>
</xsl:template>
<xsl:template match="output">
<xsl:param name="exclude" tunnel="yes"/>
<xsl:copy>
<xsl:apply-templates select="#*, node() except $exclude"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If needed or wanted you can of course wrap each xsl:next-match I have in an xsl:result-document.

Related

XSLT 3 multi-step transformation

Building from the transformation in this post, I'm now trying to integrate it into a two step transformation where the same node is transformed twice. Tested independent of each other, the transformations work. For reasons I don't understand, when I bring them together using modes, it's not going through the steps correctly - somehow the modes and variables are not aligned correctly? Fiddle here.
Given this XML:
<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="MS609-1577">
<teiHeader/>
<text>
<body>
<ab xml:id="MS609-1577-LA" xml:lang="la">
<seg type="dep_event" subtype="sighting" xml:id="MS609-1214-1"><pb break="y" n="80r"/><lb break="y" n="1"/>Item. <date type="deposition_date" when="1245-07-11" xml:id="MS609-1214_depdate">Anno Domini M°CC°XL°V° II° Ydus Junii</date>.
<persName ref="#peire_de_saint-michel" role="dep">P<supplied reason="abbr-name">etrus</supplied> de Sancto Michaele, miles</persName>, testis juratus dixit quod vidit apud
<placeName ref="#laurac_aude" type="sighting_loc">Laurac
<persName ref="#heretics_in_public" role="her">hereticos</persName><lb break="y" n="2"/>publice manentes</placeName>
set nullam familiari<del type="expunctus" rend="after">a</del>tatem habuit cum eis. <date type="sighting_date" when="1225" datingPoint="#MS609-1214_depdate" unit="y" interval="-20">Et sunt XX anni vel circa</date>.</seg>
</ab>
</body>
</text>
</TEI>
My objective is to transform this fragment:
<date type="deposition_date" when="1245-07-11" xml:id="MS609-1214_depdate">Anno Domini M°CC°XL°V° II° Ydus Junii</date>.
Into this ('moving' some text and applying analyze-string to the same node) :
<date type="deposition_date" when="1245-07-11" xml:id="MS609-1214_depdate">Anno Domini M<hi rend="sup">o</hi>CC<hi rend="sup">o</hi>XL<hi rend="sup">o</hi>V<hi rend="sup">o</hi> II<hi rend="sup">o</hi> Ydus Junii.</date>
And the rest copy without changes.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="tei"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="no"/>
<xsl:template match="/">
<xsl:variable name="step-one-result">
<xsl:apply-templates select="/" mode="step1"/>
</xsl:variable>
<xsl:apply-templates select="$step-one-result" mode="step2"/>
</xsl:template>
<xsl:template match="text()[contains(.,'°')]" mode="step1">
<xsl:analyze-string select="." regex="°">
<xsl:matching-substring>
<hi xmlns="http://www.tei-c.org/ns/1.0" rend="sup">o</hi>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="tei:date[#type='deposition_date' and ./following-sibling::node()[1][. instance of text() and starts-with(.,'.')]]" mode="step2">
<date xmlns="http://www.tei-c.org/ns/1.0">
<xsl:copy-of select="./#*"/>
<xsl:copy-of select="./(* | text())"/>
<xsl:text>.</xsl:text>
</date>
</xsl:template>
<xsl:template match="text()[preceding-sibling::node()[1][./self::tei:date[#type='deposition_date']]][starts-with(.,'.')]" mode="step2">
<xsl:value-of select="substring(.,2)"/>
</xsl:template>
</xsl:stylesheet>
Many thanks in advance.
As you are pushing the whole tree through your modes, I think you forgot to declare
<xsl:mode name="step1" on-no-match="shallow-copy"/>
<xsl:mode name="step2" on-no-match="shallow-copy"/>

Is there a way to replace the for-each with apply-templates in an XSLT Transform?

Environment: XSLT 1.0
The transform will take each element in partOne section and lookup #field attribute in partTwo section using #find attribute and then output #value attribute.
I'm using a for-each loop and was wondering if apply-templates could work?
xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="file.xslt"?>
<xml>
<partOne>
<target field="hello"/>
<target field="world"/>
</partOne>
<partTwo>
<number input="2" find="hello" value="valone" />
<number input="2" find="world" value="valtwo" />
<number input="2" find="hello" value="valthree" />
<number input="2" find="world" value="valfour" />
</partTwo>
</xml>
xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/xml/partOne/target">
,<xsl:value-of select="#field"/>
<xsl:for-each select="/xml/partTwo/number[#find=current()/#field]">
,<xsl:value-of select="#value"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
,hello
,valone
,valthree
,world
,valtwo
,valfour
Well, it seems straight-forward to change
<xsl:for-each select="/xml/partTwo/number[#find=current()/#field]">
,<xsl:value-of select="#value"/>
</xsl:for-each>
to
<xsl:apply-templates select="/xml/partTwo/number[#find=current()/#field]"/>
with a template
<xsl:template match="partTwo/number">
,<xsl:value-of select="#value"/>
</xsl:template>
As your root template so far processes all elements you need to change it to
<xsl:template match="/">
<xsl:apply-templates select="xml/partOne"/>
</xsl:template>
to avoid processing the partTwo element(s) twice.
For the cross-reference you might want to use a key in both versions:
<xsl:key name="ref" match="partTwo/number" use="#find"/>
and then select="key('ref', #field)" instead of select="/xml/partTwo/number[#find=current()/#field]" for the apply-templates or for-each.

XSLT - Copy all nodes and add extra node in copied nodes

I am looking for XSLT(1.0) code for below input and output XML.
In output XML, there can be any child node under C6 element. In below XML, i have put CN element but it could be any name.
Input XML -
<?xml version = "1.0" encoding = "UTF-8"?>
<root>
<input>
<c2>
<c3>
<c4>c4</c4>
</c3>
</c2>
</input>
<output>
<c5>
<c6>
<CN>
<T1></T1>
<T2></T2>
</CN>
</c6>
<c6>
<CN>
<T1></T1>
<T2></T2>
</CN>
</c6>
</c5>
</output>
</root>
Desired Output XML-
<root>
<output>
<c5>
<c6>
<!-- It could have any child node. Putting an example with CN child node name.-->
<CN>
<T1></T1>
<T2></T2>
<c3>
<c4>c4</c4>
<NewNode>current number of CN node which will be 1</NewNode>
<NewNode1>total number of C6 nodes which will be 2.</NewNode1>
</c3>
</CN>
</c6>
<c6>
<CN>
<T1></T1>
<T2></T2>
<c3>
<c4>c4</c4>
<NewNode>current number of CN node which will be 2</NewNode>
<NewNode1>total number of C6 nodes which will be 2.</NewNode1>
</c3>
</CN>
</c6>
</c5>
</output>
</root>
Thank you in advance.
Use the following stylesheet:
<?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" encoding="UTF-8" indent="yes" />
<xsl:template match="c6/*">
<xsl:copy>
<xsl:variable name="v1" select="count(../preceding-sibling::*)+1"/>
<xsl:variable name="v2" select="count(../../*)"/>
<xsl:apply-templates/>
<xsl:apply-templates select="../../../../input/c2/c3">
<xsl:with-param name="v1" select="$v1"/>
<xsl:with-param name="v2" select="$v2"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="c3">
<xsl:param name="v1"/>
<xsl:param name="v2"/>
<xsl:copy>
<xsl:apply-templates/>
<NewNode><xsl:value-of select="$v1"/></NewNode>
<NewNode1><xsl:value-of select="$v2"/></NewNode1>
</xsl:copy>
</xsl:template>
<xsl:template match="input"/>
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>

call template twice with similar parameters

right now I have an xml file like this:
Basically all tags appear twice, but with prefixes sideA or sideB
<root>
<sideA_foo>abc</sideA_foo>
<sideA_bar>123</sideA_bar>
<sideA_foobar>xyz</sideA_foobar>
<!--many more sideA... tags -->
<sideB_foo>def</sideB_foo>
<sideB_bar>456</sideB_bar>
<sideB_foobar>uvw</sideB_foobar>
<!--many more sideB... tags -->
</root>
then I have a template like this
<xsl:template name="template1">
<xsl:param name = "foo"/>
<xsl:param name = "bar"/>
<xsl:param name = "foobar"/>
<!-- many more params -->
<!-- do anything here -->
</xsl:template>
Is there an elegant way to call this template twice with all of its params,
<xsl:with-param name = "foo" select = "sideA_foo"/> etc.
<xsl:with-param name = "foo" select = "sideB_foo"/> etc.
without wirting all of this very verbosely, which I hate?
Here's one way you could consider:
Given an example input:
<root>
<sideA_width>5</sideA_width>
<sideA_length>7</sideA_length>
<sideB_width>6</sideB_width>
<sideB_length>3</sideB_length>
</root>
the following stylesheet:
<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">
<output>
<xsl:call-template name="side-area">
<xsl:with-param name="side" select="'sideA'"/>
</xsl:call-template>
<xsl:call-template name="side-area">
<xsl:with-param name="side" select="'sideB'"/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="side-area">
<xsl:param name="side"/>
<xsl:param name="width" select="*[name()=concat($side, '_width')]"/>
<xsl:param name="length" select="*[name()=concat($side, '_length')]"/>
<xsl:element name="{$side}_area">
<xsl:value-of select="$width * $length"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
will return:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<sideA_area>35</sideA_area>
<sideB_area>18</sideB_area>
</output>
Note, however, that explicit naming of elements is more efficient - sometimes much more efficient. The really elegant solution would be to normalize your input before it gets to you, so that it looks more like (for example):
<root>
<rect id="X">
<width>5</width>
<length>7</length>
</rect>
<rect id="Y">
<width>6</width>
<length>3</length>
</rect>
</root>

A sequence of more than one item is not allowed as the second argument of concat()

The below xsl works fine if I do not bring in the "other_location_postal_code" field, which is commented here.
This is because there are multiple records if I bring in that field.
How can I have this xsl evaluate each record so it would write this record twice, once for the one "other location postal code" and the other?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:e="http://www.taleo.com/ws/tee800/2009/01" xmlns:fct="http://www.taleo.com/xsl_functions" exclude-result-prefixes="e fct">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:param name="OUTBOUND_FOLDER"/>
<xsl:template match="/">
<source>
<xsl:apply-templates select="//e:Requisition"/>
</source>
</xsl:template>
<xsl:template match="e:Requisition">
<xsl:variable name="job_id" select="e:ContestNumber"/>
<xsl:variable name="other_location_postal_code" select="e:JobInformation/e:JobInformation/e:OtherLocations/e:Location/e:NetworkLocation/e:NetworkLocation/e:ZipCode"/>
<job>
<job_id>
<xsl:value-of select="concat('<','![CDATA[',$job_id,']]','>')"/>
</job_id>
<other_location_postal_code>
<xsl:value-of select="concat('![CDATA[',$other_location_postal_code,']]')"/>
</other_location_postal_code>
</job>
</xsl:template>
</xsl:stylesheet>
I want it to come out like so:
<?xml version="1.0" encoding="UTF-8"?>
<source>
<job>
<job_id><![CDATA[15000005]]></job_id>
<other_location_postal_code><![CDATA[77382]]></other_location_postal_code>
</job>
<job>
<job_id><![CDATA[15000005]]></job_id>
<other_location_postal_code><![CDATA[37567]]></other_location_postal_code>
</job>
</source>
The initial XML looks like so:
<?xml version="1.0" encoding="UTF-8"?>
-<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
-<soapenv:Body>
-<ns1:getDocumentByKeyResponse xmlns:ns1="http://www.taleo.com/ws/integration/toolkit/2005/07" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
-<Document xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07">
-<Attributes>
<Attribute name="count">1</Attribute>
<Attribute name="duration">0:00:00.088</Attribute>
<Attribute name="entity">Requisition</Attribute>
<Attribute name="mode">T-XML</Attribute>
<Attribute name="version">http://www.taleo.com/ws/tee800/2009/01</Attribute>
</Attributes>
-<Content>
-<ExportTXML xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:e="http://www.taleo.com/ws/tee800/2009/01">
-<e:Requisition>
<e:ContestNumber>15000005</e:ContestNumber>
-<e:JobInformation>
-<e:JobInformation>
-<e:OtherLocations>
-<e:Location>
<e:ZipCode>77002</e:ZipCode>
</e:Location>
-<e:Location>
<e:ZipCode>77050</e:ZipCode>
</e:Location>
</e:OtherLocations>
</e:JobInformation>
</e:JobInformation>
</e:Requisition>
</ExportTXML>
</Content>
</Document>
</ns1:getDocumentByKeyResponse>
</soapenv:Body>
</soapenv:Envelope>
The error message simply says that an argument to the concat function is a sequence of more than one node. So some of your variable selects two or more nodes and concat expects as single node for each of its arguments. You will need to decide what you want to output, either only the first node with $var[1] or the concatenation with string-join($var, ' ').
Note that you don't need all those attempts to output CDATA section, you can tell the XSLT processor the cdata-section-elements on the xsl:output direction.
As you have now posted more details here is one suggestion to map each ZipCode element to job element:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:e="http://www.taleo.com/ws/tee800/2009/01"
exclude-result-prefixes="e">
<xsl:output indent="yes" cdata-section-elements="job_id other_location_postal_code"/>
<xsl:template match="/">
<source>
<xsl:apply-templates select="//e:OtherLocations/e:Location/e:ZipCode"/>
</source>
</xsl:template>
<xsl:template match="e:ZipCode">
<xsl:apply-templates select="ancestor::e:Requisition">
<xsl:with-param name="zc" select="current()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="e:Requisition">
<xsl:param name="zc"/>
<job>
<job_id><xsl:value-of select="e:ContestNumber"/></job_id>
<other_location_postal_code><xsl:value-of select="$zc"/></other_location_postal_code>
</job>
</xsl:template>
</xsl:stylesheet>