对XML应用XSLT基于在同一个文件标签配对来筛选信息(Apply XSLT on XML to f

2019-10-21 02:01发布

我是新来的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>

如何编写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>

Answer 1:

最喜欢的XSLT文件,开始与身份转换 ,然后将其覆盖。

您可以通过只覆盖一个筛选出的许可证license ,其state一匹配statelicensed-states

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>

XSLT 1.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输出

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

如果你最终筛选出更多的节点比你留着,你可以切换起来,做一个xsl:apply-templates来处理更多的过滤...

XSLT 1.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 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>


文章来源: Apply XSLT on XML to filter information based on tag match in same file
标签: xml xslt