Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Xml 使用XSLT获取位置(查找特定项后的所有节点)_Xml_Xslt_Xpath - Fatal编程技术网

Xml 使用XSLT获取位置(查找特定项后的所有节点)

Xml 使用XSLT获取位置(查找特定项后的所有节点),xml,xslt,xpath,Xml,Xslt,Xpath,我正试图做这样的转变。 考虑一下我有一个XML文件: <name> <a>Andy</a> <b>Emma</b> <c>John</c> <d>Cindy</d> <e>May</e> </name> 安迪 艾玛 约翰 辛蒂 也许 现在我希望选择Emmaelement之后的所有元素,因此输出如下: <new

我正试图做这样的转变。 考虑一下我有一个XML文件:

 <name>
   <a>Andy</a>
   <b>Emma</b>
   <c>John</c>
   <d>Cindy</d>
   <e>May</e>
 </name>

安迪
艾玛
约翰
辛蒂
也许
现在我希望选择
Emma
element之后的所有元素,因此输出如下:

<new>
  <one>John</one>
  <one>Cindy</one>
  <one>May</one>
<new>

约翰
辛蒂
也许
我只能通过手动将条件声明为

[position()>2]

但有没有办法自动获得职位?大致思路如下:

<new>
  <one>John</one>
  <one>Cindy</one>
  <one>May</one>
<new>

[position()>Emma]
[position()>b]

你能做的就是
/name/*[.='Emma']/下面的兄弟姐妹::*
/name/b/下面的兄弟姐妹::*
你不需要得到这个职位,你可以使用
[前面的兄弟姐妹::*[text()='Emma']
[前面的兄弟姐妹::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:template match="/*">
     <new>
      <xsl:apply-templates select=
       "*[. = 'Emma']/following-sibling::*"/>
     </new>
 </xsl:template>

 <xsl:template match="*/*">
  <one><xsl:value-of select="."/></one>
 </xsl:template>
</xsl:stylesheet>
现在的完整转换是

<name>
    <a>Andy</a>
    <b>Emma</b>
    <c>John</c>
    <d>Cindy</d>
    <e>May</e>
</name>
<new>
   <one>John</one>
   <one>Cindy</one>
   <one>May</one>
</new>
*/*[not('Emma' = .|following-sibling::*)]
<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:template match="/*">
     <new>
      <xsl:apply-templates/>
     </new>
 </xsl:template>

 <xsl:template match="*/*[not('Emma' = .|following-sibling::*)]">
  <one><xsl:value-of select="."/></one>
 </xsl:template>
 <xsl:template match="*/*" priority="0"/>
</xsl:stylesheet>


请注意@Martin的解决方案应该比这个更有效。+1。请注意,如果有多个
Emma
元素,这两个变量将返回不同的结果。@LarsH:是的,根据问题,这是假设只有一个Emma。谢谢