I am new to XSLT, i want to move a element with all its child elements to the root as mentioned below:
Input XML
<a name = "test">
<b name = "test1">
<c name = "test2">
<c1>Dummy</c1>
</c>
</b>
</a>
Expected:
<a name = "test">
<b name = "test1">
</b>
<c name = "test2">
<c1>Dummy</c1>
</c>
</a>
Try this. (This is called push programming.)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" />
<xsl:template match="a">
<xsl:copy>
<xsl:apply-templates select="#*|b"/>
<xsl:apply-templates select="b/c"/>
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:copy>
<!-- Don't do an apply template with c in it. Just get the attributes for b. The c node pushed from the a template gets handled by the identity. -->
<xsl:apply-templates select="#*"/>
</xsl:copy>
</xsl:template>
<!-- Identity template. -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Related
I want to split this node at <foo>:
<p>
<span>This is before foo.</span>
<foo>inside foo</foo>
<span>This is after foo.</span>
</p>
The result I want is
<p>
<span>This is before foo.</span>
</p>
<foo>inside foo</foo>
<p>
<span>This is after foo.</span>
</p>
But I'm getting
<p>
<span>This is before foo.</span>
<p>
<span>This is before foo.</span>
</p>
<foo>inside foo</foo>
<p>
<span>This is after foo.</span>
</p>
<span>This is after foo.</span>
</p>
And here's the style sheet I'm trying with:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="p/foo">
<xsl:element name="p">
<xsl:apply-templates select="preceding-sibling::node()"/>
</xsl:element>
<foo>
<xsl:apply-templates/>
</foo>
<xsl:element name="p">
<xsl:apply-templates select="following-sibling::node()"/>
</xsl:element>
</xsl:template>
</xsl:transform>
How can I get rid of that extra output?
Edit: Gave a more realistic example. See http://xsltransform.net/pNP88xQ/2
-- edited in response to your edited question --
This is not an easy task to accomplish in XSLT 1.0 (it's much easier to do in XSLT 2.0 using xsl:for-each-group).
One possible approach is a method known as sibling recursion:
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" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<!-- start sibling recursion -->
<xsl:apply-templates select="node()[1]"/>
</xsl:template>
<!-- first node in a group -->
<xsl:template match="p/node()[not(self::foo)]">
<p>
<xsl:call-template name="identity"/>
<!-- collect the following node in this group -->
<xsl:apply-templates select="following-sibling::node()[1][not(self::foo)]" mode="collect"/>
</p>
<!-- continue recursion with the following divider -->
<xsl:apply-templates select="following-sibling::foo[1]"/>
</xsl:template>
<!-- other nodes in a group -->
<xsl:template match="node()" mode="collect">
<xsl:call-template name="identity"/>
<!-- collect the following node in this group -->
<xsl:apply-templates select="following-sibling::node()[1][not(self::foo)]" mode="collect"/>
</xsl:template>
<xsl:template match="foo">
<xsl:call-template name="identity"/>
<!-- restart sibling recursion -->
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
</xsl:stylesheet>
I am trying to create an empty file through xslt.
The input sample is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Businessman>
<siblings>
<sibling>John </sibling>
</siblings>
<child> Pete </child>
<child> Ken </child>
</Businessman>
When the input contains any presence of 'child' tags, it should produce the file AS IS. When the input does not have any 'child' tag, I need an empty file (0 byte file) created.
This is what I tried:
<?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" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="#*|node()">
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This gives the file unchanged when there is any 'child' tag present. But did not produce any empty file when there is no 'child' tag.
The file I need to test will look like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Businessman>
<siblings>
<sibling>John </sibling>
</siblings>
</Businessman>
Any help would be great!
Thanks
If you want the processor to go to the trouble of opening the output file, you have to give it something to write to the output file. Try an empty text node. And you only need to make the decision 'copy or not?' once.
One way to make the decision just once and produce empty output if the condition is not met would be to replace your template with:
<xsl:template match="/">
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:copy-of select="*"/>
</xsl:when>
<xsl:otherwise>
<xsl:text/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This works as expected with xsltproc. (If you find yourself getting a file containing an XML declaration and nothing else, try adjusting the parameters on xsl:output.)
But when I have found myself with a similar situation (perform this transform if condition C holds, otherwise ...), I have simply added a template for the document node that would look something like this for your case:
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:apply-templates/>
</
<xsl:otherwise>
<xsl:message terminate="yes">No children in this input, dying ...</
</
</
That way I get no output at all rather than zero-length output.
Simple enough - Just don't try to do everything in one template, don't forget to omit the xml declaration and get the xpath right:
<?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" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Businessman[child]" priority="9">
<xsl:element name="Businessman">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="Businessman" priority="0" />
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
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>
I would like to copy a parent node into the child but I'm unsure on how to proceed.
I apologize in advanced for the weird looking source file, I'm new to this forum and have no clue on how to properly paste an XML file.
My XML source file is like this:
<?xml version="1.0"?>
<IncidentLogUpload>
<Header>
<BatchID>2013</BatchID>
<SystemID>2013</SystemID>
<DateTime>12/20/2013 3:37 PM</DateTime>
</Header>
<Item>
<IncidentLogs>
<IncidentLog>
<IncidentSource>Source</IncidentSource>
<Property>Property</Property>
<Location>B1</Location>
<SubLocation/>
<DailyLogID>IN2013</DailyLogID>
<IncidentID>IN2013</IncidentID>
<Reference/>
<DateTimeOccured>12/19/2013 8:17 PM</DateTimeOccured>
<IncidentType>Surveillance</IncidentType>
<Specific>Observation</Specific>
<Category>POI</Category>
<IncidentDetails>0400</IncidentDetails>
<RelatedIncidentNo/>
<DateTimeReported>12/19/2013 8:17 PM</DateTimeReported>
<ParticipantSubjectProfiles>
<ParticipantSubjectProfile>
<FirstName>James</FirstName>
<MiddleName></MiddleName>
<LastName>Henderson</LastName>
<ParticipantType>Subject</ParticipantType>
<MembershipNumber></MembershipNumber>
<DriversLicense></DriversLicense>
<PassportNumber></PassportNumber>
<IncidentID>IN2013</IncidentID>
</ParticipantSubjectProfile>
</ParticipantSubjectProfiles>
<ParticipantPersonnelProfiles>
<ParticipantPersonnelProfile>
<BusinessUnit>Games</BusinessUnit>
<FirstName>Edison</FirstName>
<MiddleName>John</MiddleName>
<LastName>Costabile</LastName>
<CSELNumber/>
<StaffID>000408</StaffID>
<DriversLicense/>
<AffBUKey>GamesIN2013</AffBUKey>
<ParticipantType>Personnel</ParticipantType>
</ParticipantPersonnelProfile>
</ParticipantPersonnelProfiles>
</IncidentLog>
</IncidentLogs>
</Item>
<Footer>
<NumberOfRecords>5</NumberOfRecords>
</Footer>
</IncidentLogUpload>
I would like to copy the <Property> node to both <ParticipantSubjectProfile> and <ParticipantPersonnelProfile>. The end result should be like this:
<ParticipantSubjectProfiles>
<ParticipantSubjectProfile>
<FirstName>James</FirstName>
<MiddleName></MiddleName>
<LastName>Henderson</LastName>
<ParticipantType>Subject</ParticipantType>
<MembershipNumber></MembershipNumber>
<DriversLicense></DriversLicense>
<PassportNumber></PassportNumber>
<IncidentID>IN2013</IncidentID>
<Property>Property</Property>
</ParticipantSubjectProfile>
</ParticipantSubjectProfiles>
<ParticipantPersonnelProfiles>
<ParticipantPersonnelProfile>
<BusinessUnit>Games</BusinessUnit>
<FirstName>Edison</FirstName>
<MiddleName>John</MiddleName>
<LastName>Costabile</LastName>
<CSELNumber/>
<StaffID>000408</StaffID>
<DriversLicense/>
<AffBUKey>GamesIN2013</AffBUKey>
<ParticipantType>Personnel</ParticipantType>
<Property>Property</Property>
</ParticipantPersonnelProfile>
</ParticipantPersonnelProfiles>
Please help! Thank you!
Edited
<xsl:template match="/">
<ParticipantSubjectProfiles>
<xsl:for-each select="IncidentLogUpload/Item/IncidentLogs/IncidentLog/ParticipantSubjectProfiles">
<ParticipantSubjectProfile>
<FirstName>James</FirstName>
<MiddleName></MiddleName>
<LastName>Henderson</LastName>
<ParticipantType>Subject</ParticipantType>
<MembershipNumber></MembershipNumber>
<DriversLicense></DriversLicense>
<PassportNumber></PassportNumber>
<IncidentID>IN2013</IncidentID>
<Property> <xsl:value-of select="/IncidentLogUpload/Item/IncidentLogs/IncidentLog/Property"/></Property>
</ParticipantSubjectProfile>
</xsl:for-each>
</ParticipantSubjectProfiles>
</xsl:template>
</xsl:stylesheet>
Try this template out:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<ParticipantSubjectProfiles>
<xsl:for-each select="//ParticipantSubjectProfiles/ParticipantSubjectProfile">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="../preceding-sibling::Property"></xsl:apply-templates>
</xsl:copy>
</xsl:for-each>
</ParticipantSubjectProfiles>
<ParticipantPersonnelProfiles>
<xsl:for-each select="//ParticipantPersonnelProfiles/ParticipantPersonnelProfile">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="../preceding-sibling::Property"></xsl:apply-templates>
</xsl:copy>
</xsl:for-each>
</ParticipantPersonnelProfiles>
</xsl:template>
</xsl:stylesheet>
I'm getting Attribute match is not allowed on this element errors on all these elements XAML except the root. I think I'm missing something about the syntax...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates match="/Table"/>
<xsl:apply-templates match="/Paragraph"/>
</body>
</html>
</xsl:template>
<xsl:template match="Table">
<table>
<xsl:apply-templates match="TableRowGroup"/>
</table>
</xsl:template>
<xsl:template match="TableRowGroup">
<xsl:apply-templates match="TableRow"/>
</xsl:template>
<xsl:template match="TableRow">
<tr>
<xsl:apply-templates match="TableCell"/>
</tr>
</xsl:template>
<xsl:template match="TableCell">
<td>
</td>
</xsl:template>
</xsl:stylesheet>
Use <xsl:apply-templates select="..."/> instead of <xsl:apply-templates match="..."/>.