我是XSLT的新手,并试图弄清楚如何使用它来改变这个XML:
<people>
<person>
<role>Parent</role>
<lastname>Smith</lastname>
<locations>
<location>
<city>Springfield</city>
<state>MA</state>
<unimportant-field>123</unimportant-field>
</location>
<location>
<city>Chicago</city>
<state>IL</state>
<unimportant-field>456</unimportant-field>
</location>
</locations>
</person>
<person>
<role>Child</role>
<lastname>Smith</lastname>
<locations>
<location>
<city>Springfield</city>
<state>IL</state>
<unimportant-field>789</unimportant-field>
</location>
<location>
<city>Chicago</city>
<state>IL</state>
<unimportant-field>000</unimportant-field>
</location>
<location>
<city>Washington</city>
<state>DC</state>
<unimportant-field>555</unimportant-field>
</location>
</locations>
</person>
</people>
进入
<people>
<person>
<role>Parent</role>
<lastname>Smith</lastname>
<locations>
<location>
<city>Springfield</city>
<state>MA</state>
<unimportant-field>123</unimportant-field>
</location>
<location>
<city>Chicago</city>
<state>IL</state>
<unimportant-field>456</unimportant-field>
</location>
</locations>
</person>
<person>
<role>Child</role>
<lastname>Smith</lastname>
<locations>
<location>
<city>Chicago</city>
<state>IL</state>
<unimportant-field>000</unimportant-field>
</location>
</locations>
</person>
</people>
基本上正在发生的是有一个伪层次结构,我需要过滤掉“子”元素中不存在于“parent”元素中的位置。子元素通过姓氏与其父元素相关联。但是,每个位置的“不重要字段”在父节点和子节点之间不匹配,但不应影响过滤。
现在我正在尝试使用它:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/people">
<people>
<xsl:apply-templates select="person"/>
</people>
</xsl:template>
<xsl:template match="person">
<person>
<xsl:copy-of select="role" />
<xsl:copy-of select="lastname" />
<xsl:apply-templates select="locations"/>
</person>
</xsl:template>
<xsl:template match="locations">
<locations>
<xsl:apply-templates select="location[city = /people/person[role = 'Parent' and lastname = current()/../lastname]/locations/location/city and state = /people/person[role = 'Parent' and lastname = current()/../lastname]/locations/location/state]"/>
</locations>
</xsl:template>
<xsl:template match="location">
<location>
<xsl:copy-of select="city" />
<xsl:copy-of select="state" />
<xsl:copy-of select="unimportant-field" />
</location>
</xsl:template>
</xsl:stylesheet>
问题是它保留了孩子的Springfield,IL元素,因为它独立地检查城市是否存在于父母的任何地方,以及状态是否存在于父母的任何地方。我需要做的是弄清楚城市和州的组合是否存在于父母的单一位置,同时仍然忽略“不重要的领域”。我不知道怎么做。
答案 0 :(得分:0)
以这种方式试试吗?
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="*"/>
<xsl:key name="parent-location" match="person[role='Parent']/locations/location" use="concat(../../lastname, '|', city, '|', state)" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="person[role='Child']/locations/location[not(key('parent-location', concat(../../lastname, '|', city, '|', state)))]"/>
</xsl:stylesheet>