2013-10-24 45 views
0

我的目标是过滤基于通知的属性匹配值。有其如下XSLT我需要根据参数值筛选元素

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

这里,存在一些数字被输入作为PARAM 'privileges'一个xml [它意味着通知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只需要在输出中列出。

例如,输出应该是,如果它匹配2803

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

请给我一个想法。感谢

回答

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>