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_Nested - Fatal编程技术网

使用XPath查找嵌套元素(相同类型)

使用XPath查找嵌套元素(相同类型),xpath,nested,Xpath,Nested,我不知道如何找到相同类型的嵌套元素。通常,如果我有7个级别的头文件,并且想用XSLT将它们转换为h1–h7头文件,那么如何用XPath选择它们呢?我想没有什么比div/div/div/head更好的了,但这看起来真的很笨拙。这种转换: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"

我不知道如何找到相同类型的嵌套元素。通常,如果我有7个级别的头文件,并且想用XSLT将它们转换为h1–h7头文件,那么如何用XPath选择它们呢?我想没有什么比
div/div/div/head
更好的了,但这看起来真的很笨拙。

这种转换

<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="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="div/head">
      <xsl:element name="h{count(ancestor::div)}">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>
<div>
    <head>1</head>
    <div>
        <head>2-1</head>
        <div>
            <head>3-1</head>
        </div>
    </div>
    <div>
        <head>2-2</head>
    </div>
</div>
<div>
    <h1>1</h1>
    <div>
        <h2>2-1</h2>
        <div>
            <h3>3-1</h3>
        </div>
    </div>
    <div>
        <h2>2-2</h2>
    </div>
</div>

应用于此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:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="div/head">
      <xsl:element name="h{count(ancestor::div)}">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>
<div>
    <head>1</head>
    <div>
        <head>2-1</head>
        <div>
            <head>3-1</head>
        </div>
    </div>
    <div>
        <head>2-2</head>
    </div>
</div>
<div>
    <h1>1</h1>
    <div>
        <h2>2-1</h2>
        <div>
            <h3>3-1</h3>
        </div>
    </div>
    <div>
        <h2>2-2</h2>
    </div>
</div>

1.
2-1
3-1
2-2
生成所需的正确结果

<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="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="div/head">
      <xsl:element name="h{count(ancestor::div)}">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>
<div>
    <head>1</head>
    <div>
        <head>2-1</head>
        <div>
            <head>3-1</head>
        </div>
    </div>
    <div>
        <head>2-2</head>
    </div>
</div>
<div>
    <h1>1</h1>
    <div>
        <h2>2-1</h2>
        <div>
            <h3>3-1</h3>
        </div>
    </div>
    <div>
        <h2>2-2</h2>
    </div>
</div>

1.
2-1
3-1
2-2

哇,不像我想的那么容易!很有趣,谢谢。