Java 将逗号分隔的xml值转换为xml元素,并最终截断不需要的数据
我对XML和XSL非常陌生。 这是我的示例xml。我试图使用这个XSL在CAR下获取一些元素,比如Java 将逗号分隔的xml值转换为xml元素,并最终截断不需要的数据,java,xml,eclipse,xslt,Java,Xml,Eclipse,Xslt,我对XML和XSL非常陌生。 这是我的示例xml。我试图使用这个XSL在CAR下获取一些元素,比如ID、Rating和Cost <xsl:template match="/"> <SOMEDATA> <xsl:apply-templates></xsl:apply-templates> </SOMEDATA> </xsl:template> <xsl:templat
ID
、Rating
和Cost
<xsl:template match="/">
<SOMEDATA>
<xsl:apply-templates></xsl:apply-templates>
</SOMEDATA>
</xsl:template>
<xsl:template match="Car">
<DATA>
<xsl:copy-of select="Id"> </xsl:copy-of>
<xsl:copy-of select="Cost"> </xsl:copy-of>
<xsl:copy-of select="Rating"> </xsl:copy-of>
<xsl:template match="Rating">
<xsl:for-each select="tokenize(current(), ',')">
<Rate>
<xsl:value-of select="."/>
</Rate>
</xsl:for-each>
</DATA>
</xsl:template>
另外,如何通过匹配文本和逗号将逗号分隔的值转换为单独的元素?在这种情况下,标记化不起作用吗?因为我想忽略<代码>或类似的< /代码>,只考虑逗号前的值。
提前谢谢 您的输入XML不是有效的XML(缺少正确的结束标记)。更正后,可以使用以下XSLT(版本2.0):
如果您使用的是XSLT 1.0,则可以:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<SOMEDATA>
<xsl:apply-templates select="//Car"/>
</SOMEDATA>
</xsl:template>
<xsl:template match="Car">
<DATA>
<xsl:copy-of select="Id | Cost"/>
<Rating>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="Rating"/>
<xsl:with-param name="separator">,</xsl:with-param>
</xsl:call-template>
</Rating>
</DATA>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="separator" />
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<Rate>
<xsl:value-of select="normalize-space($text)"/>
</Rate>
</xsl:when>
<xsl:otherwise>
<Rate>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</Rate>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
<xsl:with-param name="separator" select="$separator"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
,
谢谢。。但是我得到了“致命错误:错误检查表达式“funcall(tokenize,[funcall(current,[]),literal expr(,)]”的类型.........其他数据仍在末尾添加您使用的XSLT的哪个版本?tokenize()在2.0中可用。如果您使用1.0,我们将不得不编写一个模板来标记Strings。我正在使用2.0。想知道为什么它不起作用。感谢您的帮助!我很感激!我想您的处理器不支持2.0。我已经用1.0样式表编辑了我的答案。这应该会有帮助。。
--------<SOMEDATA>
----------------<DATA>
-----------------------<Id> 1</Id>
-----------------------<Cost>100</Cost>
-----------------------<Rating>
---------------------------<Rate>4 star </Rate>
---------------------------<Rate>3 star </Rate>
---------------------------<Rate>2 star </Rate>
-----------------------<Rating>
----------------<DATA>
----------------<DATA>
-----------------------<Id> 2</Id>
-----------------------<Cost>200</Cost>
-----------------------<Rating>
---------------------------<Rate>5 star </Rate>
---------------------------<Rate>1 star </Rate>
-----------------------<Rating>
----------------<DATA>
----------------<DATA>
-----------------------<Id> 3</Id>
-----------------------<Cost>800</Cost>
-----------------------<Rating>
---------------------------<Rate>2 star </Rate>
---------------------------<Rate>3 star </Rate>
-----------------------<Rating>
----------------<DATA>
--------<SOMEDATA>
--------<SOMEDATA>
----------------<DATA>
-----------------------<Id> 1</Id>
-----------------------<Cost>100</Cost>
-----------------------<Rating> 4 star, 3 star, 2 star or similar </Rating>
-----------------------<Rating>
----------------<DATA>
----------------<DATA>
-----------------------<Id> 2</Id>
-----------------------<Cost>200</Cost>
-----------------------<Rating> 5 star, 1 star or similar </Rating>
----------------<DATA>
----------------<DATA>
-----------------------<Id> 3</Id>
-----------------------<Cost>800</Cost>
-----------------------<Rating> 2 star, 3 star or similar </Rating>
----------------<DATA>
USDhttp://google.comXXuuEURhttp://google.comUIRREND1END2
--------<SOMEDATA>
USDhttp://google.comXXuuEURhttp://google.comUIRREND1END2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<SOMEDATA>
<xsl:apply-templates select="//Car"/>
</SOMEDATA>
</xsl:template>
<xsl:template match="Car">
<DATA>
<xsl:copy-of select="Id | Cost"/>
<xsl:apply-templates select="Rating"/>
</DATA>
</xsl:template>
<xsl:template match="Rating">
<Rating>
<xsl:for-each select="tokenize(current(), ',')">
<Rate>
<xsl:value-of select="normalize-space(.)"/>
</Rate>
</xsl:for-each>
</Rating>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<SOMEDATA>
<xsl:apply-templates select="//Car"/>
</SOMEDATA>
</xsl:template>
<xsl:template match="Car">
<DATA>
<xsl:copy-of select="Id | Cost"/>
<Rating>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="Rating"/>
<xsl:with-param name="separator">,</xsl:with-param>
</xsl:call-template>
</Rating>
</DATA>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="separator" />
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<Rate>
<xsl:value-of select="normalize-space($text)"/>
</Rate>
</xsl:when>
<xsl:otherwise>
<Rate>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</Rate>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
<xsl:with-param name="separator" select="$separator"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>