2010-07-29 82 views
1
的谓词

另一个的XPath/XSL问题:)什么是电流()的谓词

如果我有一个节点树等:

- B(anymal类型=宠物)

---- C(类型=鸟)

---- C(类型=猫)

---- C(=型狗)

- B(工作-动物)

---- C(=型牛)

---- C(=类型大象)

-A
...

,并列出一个需要在给定anymal型类型的另一个XML文件($ XMLFILE)

-PET

----猫

----狗

木材加工动物

----大象

怎么办我只选择$ xmlFile给我的这些动物吗?:

什么在这种情况下电流()是指: - 它是该节点上的模板(“A”) 匹配 - 或者是它当前的C节点被评估。

什么是正确的方式来到达当前正在评估的C节点并且向上一步(到B,定义动物类型)。

谢谢。

+0

好问题(+1)。查看我的答案获得完整的解决方案。 – 2010-07-30 05:14:25

回答

0

这种转变

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:variable name="vrtfXmlFile"> 
    <pets> 
     <animal>cat</animal> 
     <animal>dog</animal> 
    </pets> 
    <working-animals> 
     <animal>elephant</animal> 
    </working-animals> 
    </xsl:variable> 

    <xsl:variable name="vxmlFile" select= 
    "document('')/*/xsl:variable 
         [@name='vrtfXmlFile']"/> 

<xsl:template match="node()|@*" name="identity"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="b"> 
    <xsl:if test="$vxmlFile/*[name()=current()/@animal-types]"> 
    <xsl:call-template name="identity"/> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="c"> 
    <xsl:if test= 
    "$vxmlFile/*[name()=current()/../@animal-types] 
           /animal[.=current()/@type]"> 
    <xsl:call-template name="identity"/> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

当这个XML文档施加:

<a> 
<b animal-types="pets"> 
    <c type="bird"/> 
    <c type="cat"/> 
    <c type="dog"/> 
</b> 
<b animal-types="working-animals"> 
    <c type="cow"/> 
    <c type="elephant"/> 
</b> 
</a> 

产生想要的,正确的结果

<a> 
    <b animal-types="pets"> 
     <c type="cat"/> 
     <c type="dog"/> 
    </b> 
    <b animal-types="working-animals"> 
     <c type="elephant"/> 
    </b> 
</a> 

  1. 为了方便起见,将含有允许动物第二文档,在XSLT样式表并入。在实践中,人们会使用document(some-uri)函数来读取,解析并将其定义为$vxmlFile变量的内容。

  2. 如果允许的动物列表很长,那么使用Muenchian分组(键)会更有效。