如何将XML文件压缩成一组xpath表达式?
发布时间:2020-12-16 07:59:05 所属栏目:百科 来源:网络整理
导读:考虑我有以下示例XML文件: ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/' article xmlns:ns1='http://predic8.com/material/1/' name xmlns:ns1='http://predic8.com/material/1/'foo/name description xmlns:ns1='http://pr
考虑我有以下示例XML文件:
<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'> <article xmlns:ns1='http://predic8.com/material/1/'> <name xmlns:ns1='http://predic8.com/material/1/'>foo</name> <description xmlns:ns1='http://predic8.com/material/1/'>bar</description> <price xmlns:ns1='http://predic8.com/common/1/'> <amount xmlns:ns1='http://predic8.com/common/1/'>00.00</amount> <currency xmlns:ns1='http://predic8.com/common/1/'>USD</currency> </price> <id xmlns:ns1='http://predic8.com/material/1/'>1</id> </article> </ns1:create> 什么是将其展平为一组xpath表达式的最佳(最有效)方法. 所以我想得到输出: /create/article/name /create/article/description /create/article/price/amount /create/article/price/currency /create/article/id 我正在用Java实现. 编辑: <ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'> <article xmlns:ns1='http://predic8.com/material/1/'> <name /> <description /> <price xmlns:ns1='http://predic8.com/common/1/'> <amount /> <currency xmlns:ns1='http://predic8.com/common/1/'></currency> </price> <id xmlns:ns1='http://predic8.com/material/1/'></id> </article> </ns1:create>
您可以使用XSLT轻松完成此操作.看看你的例子,看起来你只想要包含文本的元素的XPath.如果不是这样,请告诉我,我可以更新XSLT.
我创建了一个新的输入示例来展示它如何处理具有相同名称的兄弟姐妹.在这种情况下,< article>. XML输入 <ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'> <article xmlns:ns1='http://predic8.com/material/1/'> <name xmlns:ns1='http://predic8.com/material/1/'>foo</name> <description xmlns:ns1='http://predic8.com/material/1/'>bar</description> <price xmlns:ns1='http://predic8.com/common/1/'> <amount xmlns:ns1='http://predic8.com/common/1/'>00.00</amount> <currency xmlns:ns1='http://predic8.com/common/1/'>USD</currency> </price> <id xmlns:ns1='http://predic8.com/material/1/'>1</id> </article> <article xmlns:ns1='http://predic8.com/material/2/'> <name xmlns:ns1='http://predic8.com/material/2/'>some name</name> <description xmlns:ns1='http://predic8.com/material/2/'>some description</description> <price xmlns:ns1='http://predic8.com/common/2/'> <amount xmlns:ns1='http://predic8.com/common/2/'>00.01</amount> <currency xmlns:ns1='http://predic8.com/common/2/'>USD</currency> </price> <id xmlns:ns1='http://predic8.com/material/2/'>2</id> </article> </ns1:create> XSLT 1.0 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="text()"/> <xsl:template match="*[text()]"> <xsl:call-template name="genPath"/> <xsl:apply-templates select="node()|@*"/> </xsl:template> <xsl:template name="genPath"> <xsl:param name="prevPath"/> <xsl:variable name="currPath" select="concat('/',local-name(),'[',count(preceding-sibling::*[name() = name(current())])+1,']',$prevPath)"/> <xsl:for-each select="parent::*"> <xsl:call-template name="genPath"> <xsl:with-param name="prevPath" select="$currPath"/> </xsl:call-template> </xsl:for-each> <xsl:if test="not(parent::*)"> <xsl:value-of select="$currPath"/> <xsl:text>
</xsl:text> </xsl:if> </xsl:template> </xsl:stylesheet> 产量 /create[1]/article[1]/name[1] /create[1]/article[1]/description[1] /create[1]/article[1]/price[1]/amount[1] /create[1]/article[1]/price[1]/currency[1] /create[1]/article[1]/id[1] /create[1]/article[2]/name[1] /create[1]/article[2]/description[1] /create[1]/article[2]/price[1]/amount[1] /create[1]/article[2]/price[1]/currency[1] /create[1]/article[2]/id[1] UPDATE 要使XSLT适用于所有元素,只需从match =“* [text()]”中删除[text()]谓词.这将输出每个元素的路径.如果您不希望包含其他元素(如create,article和price)的元素的路径输出添加谓词[not(*)].这是一个更新的例子: 新的XML输入 <ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'> <article xmlns:ns1='http://predic8.com/material/1/'> <name /> <description /> <price xmlns:ns1='http://predic8.com/common/1/'> <amount /> <currency xmlns:ns1='http://predic8.com/common/1/'></currency> </price> <id xmlns:ns1='http://predic8.com/material/1/'></id> </article> <article xmlns:ns1='http://predic8.com/material/2/'> <name xmlns:ns1='http://predic8.com/material/2/'>some name</name> <description xmlns:ns1='http://predic8.com/material/2/'>some description</description> <price xmlns:ns1='http://predic8.com/common/2/'> <amount xmlns:ns1='http://predic8.com/common/2/'>00.01</amount> <currency xmlns:ns1='http://predic8.com/common/2/'>USD</currency> </price> <id xmlns:ns1='http://predic8.com/material/2/'>2</id> </article> </ns1:create> XSLT 1.0 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="text()"/> <xsl:template match="*[not(*)]"> <xsl:call-template name="genPath"/> <xsl:apply-templates select="node()"/> </xsl:template> <xsl:template name="genPath"> <xsl:param name="prevPath"/> <xsl:variable name="currPath" select="concat('/',$prevPath)"/> <xsl:for-each select="parent::*"> <xsl:call-template name="genPath"> <xsl:with-param name="prevPath" select="$currPath"/> </xsl:call-template> </xsl:for-each> <xsl:if test="not(parent::*)"> <xsl:value-of select="$currPath"/> <xsl:text>
</xsl:text> </xsl:if> </xsl:template> </xsl:stylesheet> 产量 /create[1]/article[1]/name[1] /create[1]/article[1]/description[1] /create[1]/article[1]/price[1]/amount[1] /create[1]/article[1]/price[1]/currency[1] /create[1]/article[1]/id[1] /create[1]/article[2]/name[1] /create[1]/article[2]/description[1] /create[1]/article[2]/price[1]/amount[1] /create[1]/article[2]/price[1]/currency[1] /create[1]/article[2]/id[1] 如果删除[not(*)]谓词,这就是输出的样子(为每个元素输出一个路径): /create[1] /create[1]/article[1] /create[1]/article[1]/name[1] /create[1]/article[1]/description[1] /create[1]/article[1]/price[1] /create[1]/article[1]/price[1]/amount[1] /create[1]/article[1]/price[1]/currency[1] /create[1]/article[1]/id[1] /create[1]/article[2] /create[1]/article[2]/name[1] /create[1]/article[2]/description[1] /create[1]/article[2]/price[1] /create[1]/article[2]/price[1]/amount[1] /create[1]/article[2]/price[1]/currency[1] /create[1]/article[2]/id[1] 这是XSLT的另一个版本,速度提高了约65%: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="text()"/> <xsl:template match="*[not(*)]"> <xsl:for-each select="ancestor-or-self::*"> <xsl:value-of select="concat('/',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/> </xsl:for-each> <xsl:text>
</xsl:text> <xsl:apply-templates select="node()"/> </xsl:template> </xsl:stylesheet> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Reactor与Proactor的比较【转载内容】
- Sqlite 扩展功能 GET_PHONEBOOK_INDEX
- ruby-on-rails – 使用find_all_by_completed(false)时,Pag
- 在页面出现flash 之后再修改allowfullscreen=true无效
- PostgreSQL CASE在函数中的用法
- 【cocos2d-x】quick-cocos2d-x 3.3final 发布
- 跟着BOY 学习COCOS2D-X 网络篇---强联网(采用技术 BSD SOC
- 正则表达式
- 如何在ruby中的方括号内提取内容
- cocos creator打包编译报错: You have not accepted the li