Skip to content

Instantly share code, notes, and snippets.

@hell0again
Created February 12, 2014 02:24
Show Gist options
  • Save hell0again/8948854 to your computer and use it in GitHub Desktop.
Save hell0again/8948854 to your computer and use it in GitHub Desktop.
xmllintとxpathのテスト
#!/bin/sh
FILE=test.xml
EXPR="string(/node/node/@*[local-name()='key'])"
echo '[prepare1. xmllint --version]'
echo $( xmllint --version )
echo ""
echo ""
echo '[prepare2. xpath]'
echo $( xpath )
echo ""
echo ""
echo '[case1. echo "xpath ${EXPR}" | xmllint --shell ${FILE} ]'
RES=$( echo "xpath ${EXPR}" | xmllint --shell ${FILE} 2>/dev/null )
if [ $(echo "${RES}" | head -c 1) = "/" ]; then
echo "${RES}" | awk '($1 == "Object"){print $6}'
else
echo "${RES}"
fi
echo ""
echo ""
echo '[case2. xmllint --xpath ${EXPR} ${FILE} ]'
xmllint --xpath "${EXPR}" ${FILE} 2>/dev/null
echo ""
echo ""
echo '[case3. xpath -e ${EXPR} ${FILE} ]'
xpath -e "${EXPR}" ${FILE} 2>/dev/null
echo ""
echo ""
echo '[case4. xpath ${FILE} ${EXPR} ]'
xpath ${FILE} "${EXPR}" 2>/dev/null
echo ""
echo ""
<node>
<node key="123456789012345678901234567890123456789012345678901234567890E"/>
<hoge>gege</hoge>
</node>
[prepare1. xmllint --version]
xmllint: using libxml version 20800
compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude Iconv ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib Lzma
[prepare2. xpath]
Usage:
/usr/bin/xpath [options] -e query [-e query...] [filename...]
If no filenams are given, supply XML on STDIN.
You must provide at least one query. Each supplementary
query is done in order, the previous query giving the
context of the next one.
Options:
-q quiet. Only output the resulting PATH
-s suffix use suffix instead of linefeed.
-p postfix use prefix instead of nothing.
[case1. echo "xpath ${EXPR}" | xmllint --shell ${FILE} ]
1234567890123456789012345678901234567890...
[case2. xmllint --xpath ${EXPR} ${FILE} ]
123456789012345678901234567890123456789012345678901234567890E
[case3. xpath -e ${EXPR} ${FILE} ]
123456789012345678901234567890123456789012345678901234567890E
[case4. xpath ${FILE} ${EXPR} ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment