XSLT过滤器是带有子元素的完整标记,但需要使用计数参考过滤子标记

时间:2014-11-06 18:58:06

标签: xml xslt

我是XML和XSLT的新手。

目前我正在尝试使用XSLT从XML文件中过滤一些信息。

这是我的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过滤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>

   

有人可以指导我如何实施计数。

1 个答案:

答案 0 :(得分: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>