2
可能重复:
Generate/get xpath from XML node java在XSLT中选择并追加当前节点的文本值?
这是从previous question我问一个后续的问题,(我决定问作为一个单独的问题,如前似乎越来越大)。
我已经得到了下面的XSLT,这让我改变一个指定的XML字符串转换为一组XPath表达式:
<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>
</xsl:text>
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>
例如,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/‘>AUD</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>
会转化为:
/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] , AUD
/create[1]/article[1]/id[1] , 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]
我的问题: 怎么样,我可以修改XSLT,这样我也可以选择并追加当前节点的文本值。还注意到,因为我提供的示例XML文件中已经说明,一些节点可能不包含文本值等
因此,对于上面的例子,我希望类似的信息(如逗号分隔输出):
/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] , some name
/create[1]/article[2]/description[1] , some description
/create[1]/article[2]/price[1]/amount[1] , 00.01
/create[1]/article[2]/price[1]/currency[1] , USD
/create[1]/article[2]/id[1] , 2
**答案就在这里提供**:http://stackoverflow.com/questions/4746299/generate-get-xpath-from-xml-node-java只要使用它。我了解用于测试XSLT的应用程序,该应用程序使用它作为测试任何XSLT转换的核心。 – 2012-07-19 15:28:35
谢谢,这帖子是相当辉煌......希望我这件事早知道! – Larry 2012-07-19 15:41:55
是的,当然,它肯定看起来强大的解决方案,解决了更多我的完整的问题(+1)的。 – Larry 2012-07-19 15:56:10