2014-11-06 1 views
0

Я новичок в XML и XSLT.XSLT Filter - полный тег с дочерними элементами, но для фильтрации дочернего тега с использованием ссылки count

В настоящее время я пытаюсь отфильтровать некоторую информацию из XML-файла с помощью XSLT.

Это мой XML-файл:

<?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> 
      <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> 
    <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>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> 
      <code>5124</code> 
      <number>14920329324</number> 
      <licensed-states> 
       <state>IL</state> 
       <state>NY</state> 
       <state>CA</state> 
      </licensed-states> 
     </appointment-info> 
     <appointment-info> 
      <code>5124</code> 
      <number>14920329324</number> 
      <licensed-states> 
       <state>NY</state> 
      </licensed-states> 
     </appointment-info> 
    </appointments> 
</Person> 
</People> 

Это мой 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> 

Это, как я получаю выход, который не является правильным,

<?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> 
     <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> 
    <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> 

В основном что я пытаюсь сделать, только фильтруйте <state>CA</state> от appointment-info/licensed-states, если человек является n ot имеет лицензию для этого состояния в licenses/license/state.

И фильтр <appointment-info>, если это единственное состояние.

В настоящее время происходит то, что для третьего лица в файле XML его фильтрация <appointment-info>, но я хочу, чтобы он только отфильтровывал <state></state>, который не совпал.

Реализация только терпит неудачу, когда имеются множественные <state> в лицензированных государств

Это, как я хочу выходной быть: Я не уверен, как реализовать счетчик на моей текущей реализации.

<?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> 
     <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> 
    <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> 
     <code>5124</code> 
     <number>14920329324</number> 
     <licensed-states> 
      <state>IL</state> 
     </licensed-states> 
    </appointment-info> 
    </appointments> 

Может кто-то наставит меня, как осуществить подсчет.

ответ

2

Трудно понять ваше описание. Будет ли следующая таблица стилей работать для вас? Это делает эти две вещи:

  • любое назначение, если лицо не имеет лицензии для любого состояний, перечисленных в назначении полностью удаляется;
  • , если лицо имеет лицензию на , некоторые из штатов, перечисленных в назначении, то назначение сохраняется, а состояния, для которых это лицо не лицензии удалены из списка.

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="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> 
+0

Это именно то, что я пытался сделать. Большое вам спасибо за то, что вы помогли мне, вы помогли мне с еще несколькими вопросами. Я хочу сказать, что вы действительно осведомлены и разбираетесь в XSLT. Спасибо. – Aniks

Смежные вопросы