【问题标题】:XPath working with empty nodesXPath 处理空节点
【发布时间】:2012-06-28 23:45:05
【问题描述】:

如果我得到了这个 XML(第一个标签是空的)

<messages>
    <message>                                                                                             
            <name>Kiro</name>                                                                       
            <acc></acc>                                                                     
            <date>20120506</date>                                                                       
            <template>TMPL_ACC_UP</template>                                                                    
        </message>                                                              
        <message>                                                                       
            <name>Third</name>                                                                  
            <acc>555</acc>                                                                  
            <date>20120507</date>                                                                       
            <template>TMPL_ACC_UP</template>                                                                
        </message>                                                              
</messages>

现在我使用这个 XPath 表达式获取所有帐户:“//message/acc/text()”,并获取节点列表 1(不包括空节点)。我想为空标签 ['555',''] 获取空字符串。我怎样才能做到这一点?谢谢!

【问题讨论】:

    标签: java xml xpath


    【解决方案1】:

    在 XPath 2.0 中可以使用这样的单行代码

    /*/message/acc/string()
    

    但是,这不能作为评估单个 XPath 1.0 表达式的结果而产生

    如果必须使用 XPath 1.0,解决方案是选择所有 /*/message/acc 元素,然后在托管 XPath 的 PL 中,输出每个选定元素的 字符串值。 p>

    例如,这个 XSLT 1.0 转换

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:template match="/">
      <xsl:text>[</xsl:text><xsl:apply-templates/><xsl:text>]</xsl:text>
     </xsl:template>
     <xsl:template match="acc">
        <xsl:if test="preceding::acc">, </xsl:if>
       <xsl:text>'</xsl:text><xsl:value-of select="."/><xsl:text>'</xsl:text>
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时:

    <messages>
        <message>
                <name>Kiro</name>
                <acc></acc>
                <date>20120506</date>
                <template>TMPL_ACC_UP</template>
            </message>
            <message>
                <name>Third</name>
                <acc>555</acc>
                <date>20120507</date>
                <template>TMPL_ACC_UP</template>
            </message>
    </messages>
    

    产生想要的正确结果:

    ['', '555']
    

    【讨论】:

      【解决方案2】:

      我会遍历节点并将它们的文本提取为值

      <?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="/">
        <html>
        <body>[
            <xsl:for-each select="//message/acc">
             '<xsl:value-of select="text()"/>',
            </xsl:for-each>]
        </body>
        </html>
      </xsl:template>
      </xsl:stylesheet>
      

      结果:[ '', '555', ]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-16
        • 2012-07-29
        • 1970-01-01
        相关资源
        最近更新 更多