XPATH 中的解析表

发布于 2024-10-20 10:26:55 字数 415 浏览 4 评论 0原文

<TR>
 <TD>Field 1</TD>
 <TD colSpan=2>Field 2</TD>
 <TD>Field 3</TD>
</TR>
<TR>
 <TD>Value for Field1</TD>
 <TD colSpan=2>Value for Field2</TD>
 <TD>Value for Field3</TD>
<TR></TR>
</TR>

如何将字段与其值映射?我正在使用 XPATH 来解析我的 html 文件。

主要问题是每个输入中的字段数量都会发生变化..但正如您所知,布局将是相同的....

<TR>
 <TD>Field 1</TD>
 <TD colSpan=2>Field 2</TD>
 <TD>Field 3</TD>
</TR>
<TR>
 <TD>Value for Field1</TD>
 <TD colSpan=2>Value for Field2</TD>
 <TD>Value for Field3</TD>
<TR></TR>
</TR>

How can i map the Field with its value? I am using XPATH to parse my html file.

And the main problem is the number of fields change in each and every input..But as you know the layout will be same....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

祁梦 2024-10-27 10:26:55

这将选择您的字段名称:

//table/tr[position() mod 2 = 0]/td/text()

这将选择您的字段值:

//table/tr[position() mod 2 = 1]/td/text()

This will select you field names:

//table/tr[position() mod 2 = 0]/td/text()

This will select you field values:

//table/tr[position() mod 2 = 1]/td/text()
枯寂 2024-10-27 10:26:55

此 XPath 表达式

/*/*[2]/TD
         [position()
         =
         count(/*/*[1]
                    /TD[.=$pName]/preceding-sibling::TD
              ) +1
         ]

选择“值”对应于变量 pName 中指定的“名称”的 TD

因此,如果您替换 $pName 在上述带有 'Field 2' 的表达式中,将选择以下内容:

<TD colSpan="2">Value for Field2</TD>

注意:即使在这种情况下,此 XPath 表达式也会选择正确的节点当两行有不同数量的 TD 时。

要仅选择 TD 的文本子节点,请将 /text() 附加到表达式中。

要仅获取字符串值,请使用

  string(/*/*[2]/TD
             [position()
             =
             count(/*/*[1]
                        /TD[.=$pName]/preceding-sibling::TD
                  ) +1
             ]
        )

这是一个简短的 XSLT 转换,证明已检索到所需的值

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:param name="pName" select="'Field 2'"/>

 <xsl:template match="/">
  <xsl:value-of select=
   "/*/*[2]/TD
             [position()
             =
             count(/*/*[1]
                        /TD[.=$pName]/preceding-sibling::TD
                  ) +1
             ]
   "/>
 </xsl:template>
</xsl:stylesheet>

当针对提供的 XML 文档应用此转换时(固定为格式良好):

<TABLE>
    <TR>
        <TD>Field 1</TD>
        <TD colSpan="2">Field 2</TD>
        <TD>Field 3</TD>
    </TR>
    <TR>
        <TD>Value for Field1</TD>
        <TD colSpan="2">Value for Field2</TD>
        <TD>Value for Field3</TD>
        <TR></TR></TR>
</TABLE>

产生了想要的结果:

Value for Field2

This XPath expression:

/*/*[2]/TD
         [position()
         =
         count(/*/*[1]
                    /TD[.=$pName]/preceding-sibling::TD
              ) +1
         ]

selects the TD with "value" corresponding to the "name" specified in the variable pName

Thus, if you substitute $pName in the above expression with 'Field 2',the following will be selected:

<TD colSpan="2">Value for Field2</TD>

Note: This XPath expression selects the correct node even in the case when the two rows have different number of TDs.

To select only the text child node of the TD, append /text() to the expression.

To get just the string value, use:

  string(/*/*[2]/TD
             [position()
             =
             count(/*/*[1]
                        /TD[.=$pName]/preceding-sibling::TD
                  ) +1
             ]
        )

Here is a short XSLT transformation proving that the wanted value is retrieved:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:param name="pName" select="'Field 2'"/>

 <xsl:template match="/">
  <xsl:value-of select=
   "/*/*[2]/TD
             [position()
             =
             count(/*/*[1]
                        /TD[.=$pName]/preceding-sibling::TD
                  ) +1
             ]
   "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied against the provided XML document (fixed to be made well-formed):

<TABLE>
    <TR>
        <TD>Field 1</TD>
        <TD colSpan="2">Field 2</TD>
        <TD>Field 3</TD>
    </TR>
    <TR>
        <TD>Value for Field1</TD>
        <TD colSpan="2">Value for Field2</TD>
        <TD>Value for Field3</TD>
        <TR></TR></TR>
</TABLE>

the wanted result is produced:

Value for Field2
淡水深流 2024-10-27 10:26:55

使用此输入:

<TABLE>
    <TR>
        <TD>Field 1</TD>
        <TD colSpan="2">Field 2</TD>
        <TD>Field 3</TD>
    </TR>
    <TR>
        <TD>Value for Field1</TD>
        <TD colSpan="2">Value for Field2</TD>
        <TD>Value for Field3</TD>
    </TR>
</TABLE>

此 XPath 表达式:

/TABLE/TR/TD[.='Field 3']/following::TD[count(../TD)]

它选择此元素:

<TD>Value for Field3</TD>

注意:这假设有一个“常规”表。

With this input:

<TABLE>
    <TR>
        <TD>Field 1</TD>
        <TD colSpan="2">Field 2</TD>
        <TD>Field 3</TD>
    </TR>
    <TR>
        <TD>Value for Field1</TD>
        <TD colSpan="2">Value for Field2</TD>
        <TD>Value for Field3</TD>
    </TR>
</TABLE>

This XPath expression:

/TABLE/TR/TD[.='Field 3']/following::TD[count(../TD)]

It selects this element:

<TD>Value for Field3</TD>

Note: This assumes a "regular" table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文