在XML上应用XSLT以根据同一文件中的标记匹配过滤信息

在XML上应用XSLT以根据同一文件中的标记匹配过滤信息,xml,xslt,Xml,Xslt,我不熟悉XML和XSLT,我想从XML文件中过滤一些信息。基于XML文件中某些标记值的匹配 这是我的XML文件,如下所示: <?xml version="1.0" encoding="UTF-8"?> <People> <Person> <required-tag1>some-information</required-tag1> <required-tag2>some-information</req

我不熟悉XML和XSLT,我想从XML文件中过滤一些信息。基于XML文件中某些标记值的匹配

这是我的XML文件,如下所示:

<?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>
我想基本上做的是,如果一个人在某个州(例如TX)获得许可证,并且在该州(例如TX)拥有约会信息,则从许可证中过滤该信息。如果这是唯一的许可证信息,则过滤此人

新的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">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>

一些信息
一些信息
迈克
休伊特
938387
白细胞介素
健康
一些信息
一些信息
约翰
约翰尼
1762539
纽约
健康
一些信息
一些信息
约翰
约翰尼
840790
加利福尼亚州
健康
如何编写XSLT来过滤这些信息。我使用的是XSLT 1.0版

目前,我能够应用此XSLT来获取转换所需的标记。 但我不知道如何筛选许可证状态:

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

像大多数XSLT一样,从一个开始,然后覆盖它

您只需覆盖其
状态与
许可状态中的
状态匹配的
许可证
,即可筛选出许可证

XML输入

<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>
<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>
XSLT1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!--Identity transform (aka identity template). This will match
    and copy attributes and nodes (element, text, comment and
    processing-instruction) without changing them. Unless a more
    specific template matches, everything will get handled by this
    template.-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--This template will match all "appointments", "tag3", "tag4" element nodes.
    It will also match "license" element nodes that have a child "state"
    element whose value matches a "state" element node that is a child of 
    "licensed-states".
    It will also match the "Person" element node if the number of
    "state" elements that don't have a corresponding "licensed-state"
    is equal to zero. ("filtered person who matched all licenses"
    requirement.)
    Instead of writing 4 individual xsl:templates, I used
    the union "|" operator in the "match" attribute. Since the "xsl:template" is 
    empty, nothing is output or processed further.-->
    <xsl:template match="appointments|license[state=../..//licensed-states/state]|tag3|
    tag4|Person[count(licenses/license[not(state=../..//licensed-states/state)])=0]"/>

</xsl:stylesheet>

丹尼尔:这个很好用,但我不明白它是怎么用的,你能解释一下吗。我是XML/XSLT新手,希望学习。我在网上看到了一些教程,它们只教基本的和一些循环。在上面的解决方案中,我猜它复制了整个XML,然后对Licenses标记进行匹配,然后指定所有不希望复制的标记?我不明白标签是怎么掉下来的?非常感谢你的帮助,非常感谢。我应该使用什么资源来学习XSLT?@Aniks-我在XSLT中添加了注释。希望它们有意义。我推荐Michael Kay的XSLT书籍。它们是一个巨大的资源。我也喜欢规范(XSLT和XPath)本身。Jeni Tennison的XSLT页面非常方便()以及来自Mulberry Tech()的快速参考卡。我还推荐一个好的编辑器()来编写XSLT。这让事情变得容易多了。此外,在这里搜索SO将提供大量关于如何处理XSLT中的任何内容的示例。您指导的解决方案适用于包含两个人信息的xml文件,当我尝试使用更多人进行测试时,它失败了。除了最后一个,所有的都是空的。我不明白为什么会发生这种情况,你能在这方面给我一些指导吗。@Aniks-所有
许可证
元素都为空的原因是XSLT正在查看所有
许可状态
;不仅仅是当前
人员的
许可状态
。如果所有许可证都匹配,我还添加了一个匹配项来过滤
人员。请看我编辑的答案。谢谢你的及时回复。这个解决方案帮助我学到了很多东西,对我来说非常有用。您是否有使用XSLT将XML转换为CSV的经验。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!--Identity transform (aka identity template). This will match
    and copy attributes and nodes (element, text, comment and
    processing-instruction) without changing them. Unless a more
    specific template matches, everything will get handled by this
    template.-->    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--This template will match the "Person" element node. The "xsl:copy"
    creates the new "Person" element. The "xsl:apply-templates" tells
    the processor to apply templates to any attributes (of Person) or
    elements listed in the "select". (Other elements will not be 
    processed.) I used the union operator in the "select" so I wouldn't
    have to write multiple "xsl:apply-templates".-->
    <xsl:template match="Person">
        <xsl:copy>
            <xsl:apply-templates select="@*|first-name|last-name|
                required-tag1|required-tag2|licenses"/>
        </xsl:copy>
    </xsl:template>

    <!--This template will match any "license" element nodes that have a child 
    "state" element whose value matches a "state" element node that is a 
    child of "licensed-states". 
    This template will also match the "Person" element node if the number of
    "state" elements that don't have a corresponding "licensed-state"
    is equal to zero. ("filtered person who matched all licenses"
    requirement.)
    Since the "xsl:template" is empty, nothing 
    is output or processed further.-->
    <xsl:template match="license[state=../..//licensed-states/state]|
    Person[count(licenses/license[not(state=../..//licensed-states/state)])=0]"/>

</xsl:stylesheet>