I am new to XML and XSLT, I want to filter some information from an XML file. Based on match on some tag values in the XML file.
This is my XML File as follows:
<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<tag3>not important info</tag3>
<tag4>not important info</tag4>
<first-name>Mike</first-name>
<last-name>Hewitt</last-name>
<licenses>
<license>
<number>938387</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">TX</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
<license>
<number>938387</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">IL</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
</licenses>
<appointments>
<appointment-info>
<code>5124</code>
<number>14920329324</number>
<licensed-states>
<state>TX</state>
</licensed-states>
</appointment-info>
</appointments>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<tag3>not important info</tag3>
<tag4>not important info</tag4>
<first-name>John</first-name>
<last-name>Jhonny</last-name>
<licenses>
<license>
<number>1762539</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">TX</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
<license>
<number>1762539</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NY</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
</licenses>
<appointments>
<appointment-info>
<code>5124</code>
<number>14920329324</number>
<licensed-states>
<state>TX</state>
</licensed-states>
</appointment-info>
</appointments>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<tag3>not important info</tag3>
<tag4>not important info</tag4>
<first-name>Mike</first-name>
<last-name>Hewitt</last-name>
<licenses>
<license>
<number>17294083</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">IL</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
</licenses>
<appointments>
<appointment-info>
<code>5124</code>
<number>14920329324</number>
<licensed-states>
<state>IL</state>
</licensed-states>
</appointment-info>
</appointments>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<tag3>not important info</tag3>
<tag4>not important info</tag4>
<first-name>John</first-name>
<last-name>Jhonny</last-name>
<licenses>
<license>
<number>840790</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">TX</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
<license>
<number>840790</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NY</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
<license>
<number>840790</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">CA</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
</licenses>
<appointments>
<appointment-info>
<code>5124</code>
<number>14920329324</number>
<licensed-states>
<state>TX</state>
<state>NY</state>
</licensed-states>
</appointment-info>
</appointments>
</Person>
</People>
What I want to basically do is that, if a Person is licensed in a state for example TX. And has appointment information in that state for example TX, filter that from licenses. If that is the only license information then filter the person.
And the new xml should contain information of required tags. And only Licenses which didn't match with licenses in appointment licenses state. And filtered person who matched all licenses.
<?xml version="1.0" encoding="UTF-8"?>
<People>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>Mike</first-name>
<last-name>Hewitt</last-name>
<licenses>
<license>
<number>938387</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">IL</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
</licenses>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>John</first-name>
<last-name>Jhonny</last-name>
<licenses>
<license>
<number>1762539</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">NY</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
</licenses>
</Person>
<Person>
<required-tag1>some-information</required-tag1>
<required-tag2>some-information</required-tag2>
<first-name>John</first-name>
<last-name>Jhonny</last-name>
<licenses>
<license>
<number>840790</number>
<state xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">CA</state>
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field>
</license>
</licenses>
</Person>
</People>
How to write an XSLT to filter this information. I am using XSLT Version 1.0
Currently I am able to apply this XSLT to get the required tags for transformation. But I don't know how to filter for Licenses States:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/People">
<People>
<xsl:apply-templates select="Person"/>
</People>
</xsl:template>
<xsl:template match="Person">
<Person>
<xsl:copy-of select="required-tag1"/>
<xsl:copy-of select="required-tag2"/>
<xsl:copy-of select="first-name"/>
<xsl:copy-of select="last-name"/>
</Person>
</xsl:template>
</xsl:stylesheet>
Like most XSLTs, start with an identity transform and then override it.
You can filter out the licenses by only overriding a
license
whosestate
matches astate
in alicensed-states
.XML Input
XSLT 1.0
XML Output
If you end up filtering out more nodes than you keep, you can switch it up and do an
xsl:apply-templates
to handle more of the filtering...XSLT 1.0 (same output as above)