Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 谓词的谓词中的current()是什么_Xslt_Xpath - Fatal编程技术网

Xslt 谓词的谓词中的current()是什么

Xslt 谓词的谓词中的current()是什么,xslt,xpath,Xslt,Xpath,另一个XPath/XSL问题:) 如果我有一个节点树,如: A --B(任意类型=宠物) ----C(类型=鸟) ----C(类型=cat) ----C(类型=狗) --B(工作动物) ----C(类型=奶牛) ----C(类型=大象) -A 还有另一个xml文件($xmlFile),它列出了给定anymal类型所需的类型 -宠物 ----猫 ----狗 -工作动物 ----象 如何仅选择$xmlFile命令我选择的动物 在本例中,current()指的是什么: -模板匹配的节点是(“A”

另一个XPath/XSL问题:)

如果我有一个节点树,如:

A

--B(任意类型=宠物)

----C(类型=鸟)

----C(类型=cat)

----C(类型=狗)

--B(工作动物)

----C(类型=奶牛)

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

-A

还有另一个xml文件($xmlFile),它列出了给定anymal类型所需的类型

-宠物

----猫

----狗

-工作动物

----象

如何仅选择$xmlFile命令我选择的动物

在本例中,current()指的是什么: -模板匹配的节点是(“A”) -或者它是当前正在求值的C节点

到达当前正在评估的C节点并向上移动一个节点(到定义动物类型的B节点)的正确方法是什么


谢谢。

这一转变

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

猫
狗
大象
应用于此XML文档时

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

生成所需的正确结果

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

注释

<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>
<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>
  • 为了方便起见,包含允许的动物的第二个文档合并到XSLT样式表中。实际上,可以使用
    document(someuri)
    函数来读取、解析并将其定义为
    $vxmlFile
    变量的内容

  • 如果允许的动物名单很长,那么使用明钦族分组(键)将大大提高效率

  • 好问题(+1)。请参阅我的答案以获得完整的解决方案。