2012-07-02 22 views
4

考虑我有下面的示例XML文件:如何将XML文件压缩成一组xpath表达式?

<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中实现。

编辑: PS,我可能还需要这的情况下工作,有在文本节点没有数据,因此,例如,这下应该产生的输出同上:

<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> 
+0

我试图希望得到一些比迭代文档等手动方法更可靠的方法。例如:我们还需要考虑多元素,它可以转换为/ field [0]和/字段[1]等。 – Larry

回答

2

你可以用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>&#xA;</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()]谓语。这将输出每个元素的路径。如果不希望包含其他元素的元素(如创建,文章和价格)的路径输出添加谓词[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('/',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>&#xA;</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('/',local-name(),'[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/> 
     </xsl:for-each> 
     <xsl:text>&#xA;</xsl:text> 
     <xsl:apply-templates select="node()"/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

非常感谢您的解决方案,但是,您可以考虑在文本节点上没有数据的情况下吗?我编辑了我的问题,概述了这样的例子。日Thnx。 – Larry

+0

@Larry - 请参阅我的更新。如果您有任何其他问题,请告诉我。 –

+0

我也做了一个小改动,以便任何属性值不会被转储到输出。 –

0

我的建议是使用SAX解析器。 wiki entry for SAXXerces: a SAX parser for java by Apache

在每个开始元素上,将元素的名称添加到列表的末尾。在每个结束元素上,删除最后一个列表条目。当你遇到内容,并且你想输出你的xpath时,可以通过迭代列表来检索它。