简体   繁体   English

XPath表达式忽略子节点

[英]XPath expression to ignore the child nodes

I have the following xml: 我有以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <folder>
      <TestOne>F</TestOne>
      <TestTwo>01</TestTwo>
      <case>
         <CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
         <RECORDCD>C</RECORDCD>
      </case>
      <case>
         <CRDATTIM>2014-03-26-05.05.51.531840</CRDATTIM>
         <RECORDCD>C</RECORDCD>
      </case>
   </folder>
</response>

I need the XPath expression to read the nodes and their values immediately under the <folder> tag. 我需要XPath表达式来立即在<folder>标记下读取节点及其值。 I don't require the child nodes information. 我不需要子节点信息。 I mean, my extracted info should look like below: 我的意思是,我提取的信息应如下所示:

<folder>
<TestOne>F</TestOne>
<TestTwo>01</TestTwo>
<folder>

Here the tags names listed under <folder> ie <TestOne>, <TestTwo> are dynamic. 在此处, <folder>下列出的标签名称即<TestOne>, <TestTwo>是动态的。

Is there any way to derive an XPath expression for above requirement? 有什么方法可以为上述需求派生XPath表达式? Please let me know if my question is not clear. 如果我的问题不清楚,请告诉我。 Thanks in advance. 提前致谢。

Here's a template that will output only direct childs of a <folder> node: 这是一个模板,将仅输出<folder>节点的直接子代:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/response/folder">
        <folder>
            <xsl:apply-templates select="node()" mode="childs"/>
        </folder>
    </xsl:template>
    <xsl:template match="*" mode="childs">
        <xsl:if test="count(./text()) = 1">
            <xsl:element name="{local-name()}">
                <xsl:value-of select="."/>
            </xsl:element>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

If you're just after the xpath to get all those nodes within your folder node: 如果您只是想在xpath之后获取文件夹节点中的所有那些节点,请执行以下操作:

/response/folder/node()[count(text()) = 1]

您尝试过count方法吗?

/response/folder/*[count(*)=0]

XPath is a way to select particular nodes out of an XML tree, but on its own it can't change the content of any of those nodes. XPath是一种从XML树中选择特定节点的方法,但它本身不能更改任何这些节点的内容。 If you use an expression to select the folder element 如果使用表达式选择folder元素

/response/folder

then you'll get the whole of that element, including the case children. 那么您将获得整个元素的全部内容,包括case子元素。 You could select just the "leaf" elements under folder using 您可以使用以下命令在folder下仅选择“叶子”元素

/response/folder/*[not(*)]

which would give you a node set containing two elements (the TestOne and the TestTwo ), but assembling those two elements into a new folder element is not something XPath can do for you. 这将为您提供一个包含两个元素( TestOneTestTwo )的节点集,但是将这两个元素组合成一个新的 folder元素并不是XPath可以为您做的。 You'll have to do that yourself using whatever facilities are provided by the language/tool you're using to evaluate the XPath expressions. 您必须自己使用用于评估XPath表达式的语言/工具提供的任何功能来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM