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
元素序列的xpath层次结构_Xpath_Hierarchy - Fatal编程技术网

元素序列的xpath层次结构

元素序列的xpath层次结构,xpath,hierarchy,Xpath,Hierarchy,我有以下问题: <root> <b>Value 1</b> <p>item 1</p> <p>item 2</p> <p>item 3</p> <b>Value 2</b> <p>item 1</p> <p>item 2</p> <b>value 3</b> <p>item 1&

我有以下问题:

<root>
<b>Value 1</b>
<p>item 1</p>
<p>item 2</p>
<p>item 3</p>

<b>Value 2</b>
<p>item 1</p>
<p>item 2</p>

<b>value 3</b>
<p>item 1</p>
<p>item 2</p>
<p>item 3</p>
<p>item 4</p>
</root>

有可能吗?

如果您使用的是xslt,那么这将提供一些细节,基本上选择元素,然后逐个查找前面的第一个元素

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:template match="/">
        <xsl:apply-templates select="//p"/>
    </xsl:template>

    <xsl:template match="p">
         <xsl:value-of select="preceding::b[1]"/> <xsl:value-of select="."/>
    </xsl:template>

</xsl:stylesheet>


这是可能的!给我几分钟,我给你举几个例子!谢谢你的建议,所以你确认我需要用XSLT来做,没有办法直接用XPath来做,对吗?不,这种问题不能只用XPath来解决。我认为XSLT最适合解决这些问题!
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:template match="/">
        <xsl:apply-templates select="//p"/>
    </xsl:template>

    <xsl:template match="p">
         <xsl:value-of select="preceding::b[1]"/> <xsl:value-of select="."/>
    </xsl:template>

</xsl:stylesheet>