我的目标是过滤基于通知的属性匹配值。有一个xml作为参数'privileges'
输入如下
<privileges>
<privilege>Access my notices</privilege>
<privilege>Access draft notices</privilege>
<privilege>Place notice</privilege>
<privilege>Access published notices</privilege>
<privilege>Place notice of type:2903</privilege>
<privilege>Place notice of type:2413</privilege>
<privilege>Place notice of type:2803</privilege>
<privilege>Access pending notices</privilege>
<privilege>Access my account</privilege>
<privilege>Place legacy notice</privilege>
<privilege>Access my searches</privilege>
</privileges>
这里有一些数字[表示通知ID],例如2413, 2803
等。
我的xml输入是
<notice-taxonomy>
<notice-type code="11" level="category" name="State" popular="true" service-key="all-notices" sort="01">
<notice-type code="1101" level="notice" name="Proclamations" sort="01"/>
<notice-type code="2803" level="notice" name="Royal Family" sort="02"/>
<notice-type code="1103" level="notice" name="Appointments to the Royal Household" sort="03"/>
<notice-type code="1104" level="notice" name="Loyal Addresses" sort="04"/>
<notice-type code="2413" level="notice" name="Honours and Awards" sort="05"/>
<notice-type code="1106" level="notice" name="Privy Council Office" sort="06"/>
<notice-type code="2413" level="notice" name="Change of Name and/or Arms" sort="07"/>
<notice-type code="1108" level="notice" name="Crown Office" sort="08"/>
<notice-type code="1109" level="notice" name="Duchy of Cornwall or Duchy of Lancaster" sort="09"/>
</notice-taxonomy>
我的xslt是
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="privileges" as="node()" select="doc('privileges.xml')"/>
<xsl:variable name="codeps" select="substring-after($privileges//privilege, ':')"/>
<xsl:output encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="notice-taxonomy">
<notice-taxonomy>
<xsl:for-each-group select="notice-type" group-by="@code = $codeps">
<xsl:sequence select="." />
</xsl:for-each-group>
</notice-taxonomy>
</xsl:template>
</xsl:stylesheet>
无论code
属性与参数值[从preveliges
提取的数字]匹配,那么,只需要在输出中列出那些notice-type
。
<notice-taxonomy>
<notice-type code="2803" level="notice" name="Royal Family" sort="02"/>
<notice-type code="2803" level="notice" name="Change of Name and/or Arms" sort="07"/>
</notice-taxonomy>
如果匹配2413
<notice-taxonomy>
<notice-type code="2413" level="notice" name="Royal Family" sort="02"/>
<notice-type code="2413" level="notice" name="Change of Name and/or Arms" sort="07"/>
</notice-taxonomy>
如果两者都匹配,
<notice-taxonomy>
<notice-type code="2803" level="notice" name="Royal Family" sort="02"/>
<notice-type code="2803" level="notice" name="Change of Name and/or Arms" sort="07"/>
<notice-type code="2413" level="notice" name="Crown Office" sort="08"/>
<notice-type code="1413" level="notice" name="Duchy of Cornwall or Duchy of Lancaster" sort="09"/>
</notice-taxonomy>
请给我一个主意。感谢
答案 0 :(得分:2)
将变量定义为
<xsl:variable name="codeps" select="$privileges//privilege[matches(., ':[0-9]+$')]/substring-after(., ':')"/>
然后
<xsl:key name="k1" match="notice-type" use="@code"/>
和
<xsl:template match="notice-taxonomy">
<notice-taxonomy>
<xsl:copy-of select="key('k1', $codeps)"/>
</notice-taxonomy>
</xsl:template>