I have a XML which have two parent nodes (Base, Sub). I need to write a XSLT to get the values for below condition.
Condition: Need to get different elements in both parents.
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Base>
<Student_ID>1234</Student_ID>
<Student_ID>1267</Student_ID>
<Student_ID>1890</Student_ID>
<Student_ID>5678</Student_ID>
<Student_ID>6743</Student_ID>
<Student_ID>8743</Student_ID>
</Base>
<Sub>
<Student_ID>5678</Student_ID>
<Student_ID>6743</Student_ID>
<Student_ID>3226</Student_ID>
<Student_ID>8123</Student_ID>
</Sub>
</Data>
Expected Output:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Student_ID>1234</Student_ID>
<Student_ID>1267</Student_ID>
<Student_ID>1890</Student_ID>
<Student_ID>8743</Student_ID>
<Student_ID>3226</Student_ID>
<Student_ID>8123</Student_ID>
</Data>
CodePudding user response:
Maybe something like this?
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="student" match="Student_ID" use="." />
<xsl:template match="/Data">
<xsl:copy>
<xsl:copy-of select="Base/Student_ID[not(key('student', ., ../../Sub))]"/>
<xsl:copy-of select="Sub/Student_ID[not(key('student', ., ../../Base))]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
CodePudding user response:
Another way you could look at it:
XSLT 2.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:template match="/Data">
<xsl:copy>
<xsl:for-each-group select="*/Student_ID" group-by=".">
<xsl:if test="count(current-group())=1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Note that this assumes there are no duplicates within each branch.