1

我有一个格式如下的 XML

<config>
        <global> 
                <log>
                        <file>log/global.log</file>
                        <level>DEBUG</level>
                </log>
        </global>
        <pipeline name="MediaExchange">
                <stop-file>MediaExchange.STOP</stop-file>
                <!--Delay in seconds before each poll-->
                <poll-delay>30</poll-delay>
                <source type="Cardinal-CH5-Meta">
                        <passive-mode>true</passive-mode>
                        <username>cardinalch5</username>                       
                </source>
            ....

我正在尝试打印标签中的值,然后用其他值替换它。我编写了下面的代码来打印标签中的值

# Retrieve value from XML file
xpath()
{
        log DEBUG $scriptFile "Parsing XML xpath"
        if [ `echo $1 | grep -v grep | grep /@ | wc -l` -ne 0 ]; then
                argDelta=`echo $1 | sed ' s!.*/@!! ; s/"$// '`
                value=`echo "cat $1" | xmllint --shell $2 | grep -v grep | grep = | sed -n 's|'${argDelta}'="\(.*\)"|\1|p'`
        else
                argDelta=`echo $1 | sed ' s!.*/!! ; s/"$// '`
                value=`echo "cat $1" | xmllint --shell $2 | sed -n 's|<'${argDelta}'>\(.*\)</'${argDelta}'>|\1|p'`
        fi
        echo $value
}
username=$(xpath /config/pipeline/[@name=\'MediaExchange\']/source/[@type=\'Cardinal-CH5-Meta\']/username config/config.xml)

我得到了以下异常

XPath error : Invalid expression
/config/pipeline/[@name='MediaExchange']/source/[@type='Cardinal-CH5-Meta']/username
                 ^
xmlXPathEval: evaluation failed
/config/pipeline/[@name='MediaExchange']/source/[@type='Cardinal-CH5-Meta']/username: no such node

有人可以帮忙吗?

4

1 回答 1

0

您的 Xpath 查询不正确,请看:

/config/pipeline[@name='MediaExchange']/source[@type='Cardinal-CH5-Meta']/username/text()
于 2013-07-30T12:01:57.670 回答