2014-11-06 73 views
0

我是XML和XSLT的新手。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如果該人是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 2014-11-06 20:21:47