some-information s" name="description" /> some-information s" />
43701

XSLT Filter's the complete tag, with child elements but need to filter an child tag using the c

Question:

I am new to XML and XSLT.

Currently I am trying to filter some information from an XML file using XSLT.

This is my XML file:

<?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">TX</state> <field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field> </license> </licenses> <appointments> <appointment-info> 5124 <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> <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> 5124 <number>14920329324</number> <licensed-states> <state>CA</state> </licensed-states> </appointment-info> </appointments> </Person> <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>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> 5124 <number>14920329324</number> <licensed-states> <state>IL</state> <state>NY</state> <state>CA</state> </licensed-states> </appointment-info> <appointment-info> 5124 <number>14920329324</number> <licensed-states> <state>NY</state> </licensed-states> </appointment-info> </appointments> </Person> </People>

This is my XSLT:

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="appointment-info[licensed-states/state!=ancestor::Person/licenses/license/state]"/> </xsl:stylesheet>

This is how I am getting the output which is not correct,

<?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">TX</state> <field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field> </license> </licenses> <appointments> <appointment-info> 5124 <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> <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/> </Person> <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>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/> </Person> </People>

Basically what I am trying to do is, only filter the <state>CA</state> from appointment-info/licensed-states if the person is not having license for that state in licenses/license/state.

And filter <appointment-info> if that's the only state.

Currently what is happening is that for third person in the XML file, its filtering <appointment-info> but I want it to only filter the <state></state> which didn't match.

The implementation only fails when there are multiple <state> in licensed-states

This is how I want the output to be: I am not sure how to implement a count on my current implementation.

<?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">TX</state> <field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Health</field> </license> </licenses> <appointments> <appointment-info> 5124 <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> <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/> </Person> <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>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> 5124 <number>14920329324</number> <licensed-states> <state>IL</state> </licensed-states> </appointment-info> </appointments>

Can someone guide me how to implement the count.

Answer1:

It is difficult to understand your description. Would the following stylesheet work for you? It does these two things:

<ul><li>any appointment where the person is not licensed for <em>any</em> of the states listed in the appointment is removed completely;</li> <li>if a person is licensed for <em>some</em> of the states listed in the appointment, then the appointment is kept and the states for which the person is <em>not</em> licensed are removed from the list. </li> </ul>

<strong>XSLT 1.0</strong>

<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="appointment-info[not(licensed-states/state=ancestor::Person/licenses/license/state)]"/> <xsl:template match="licensed-states/state[not(.=ancestor::Person/licenses/license/state)]"/> </xsl:stylesheet>

Recommend

  • Show generated SQL in toplink in eclipse
  • Select an attribute with Xpath in a XML with namespace using Powershell
  • Xpath how to get element by index AND attribute
  • Spring custom user details service null pointer exception
  • RS-232 communication using an Arduino Duemilanove and the Cutedigi RS-232 interface
  • SessionInformation expiration doesn't lead to user log out from system
  • Reading contents of a managed bean with reflection in a JSF application
  • Aspect not executed in Spring
  • pure javascript dom dynamic insert, update and delete
  • how to ignore xml namespaces?
  • Should a web service response include empty values?
  • Feature detection of foreignObject in SVG
  • XSD with multi occurrences unordered
  • Splash Screen will not display
  • How Get arguments value using inline assembly in C without Glibc?
  • Spring: No transaction manager has been configured
  • How to make R's read_csv2() recognise the text characters properly
  • Implementation of State Monad
  • JBoss External Properties Files in Classpath
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Jetty 9 HashLoginService
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Read a local file using javascript
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • ImageMagick, replace semi-transparent white with opaque white
  • Highlight one bar in a series in highcharts?
  • does jqgrid support a multiple checkbox list for editing
  • Cannot connect to cassandra from Spark
  • Pass value from viewmodel to script in zk
  • Optimizing database types to compact database (SQLite)
  • Cross-Platform Protobuf Serialization
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Calling of Constructors in a Java
  • bootstrap to use multiple ng-app
  • PHP: When would you need the self:: keyword?
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy