java - 将逗号分隔的 xml 值转换为 xml 元素并最后截断不需要的数据

标签 java xml eclipse xslt

我对 XML 和 XSL 非常陌生。 这是我的示例 xml。我正在尝试使用此 XSL 获取 CAR 下的一些元素,例如 IDRatingCost

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

====输入====

--<MainXML>
----<Errors/>
--------<SubData>
-----------<CarData>

------------------<Cars>
---------------------<Car>
-----------------------<Id> 1</Id>
-----------------------<Name>ABC</Name>
-----------------------<Cost>100</Cost>
-----------------------<Rating> 4 star, 3 star, 2 star or similar </Rating>
---------------------<Car>

---------------------<Car>
-----------------------<Id> 2</Id>
-----------------------<Name>XYZ</Name>
-----------------------<Cost>200</Cost>
-----------------------<Rating> 5 star, 1 star or similar </Rating>
---------------------<Car>

---------------------<Car>
-----------------------<Id> 3</Id>
-----------------------<Name>DBC</Name>
-----------------------<Cost>800</Cost>
-----------------------<Rating> 2 star, 3 star or similar </Rating>
---------------------<Car>

------------------<Cars>
-----------<CarData>
--------<SubData>

--------<SubData1>
------------<SubDataRESULT1>
--------------------- <currecy>USD</currecy>
--------------------- <link> http://google.com </link>
--------------------- <Loca> XX </Loca>
--------------------- <Roca> uu </Roca>
------------<SubDataRESULT1>

------------<SubDataRESULT1>
--------------------- <currecy>EUR</currecy>
--------------------- <link> http://google.com </link>
--------------------- <Loca> UI </Loca>
--------------------- <Roca> RR </Roca>
------------<SubDataRESULT1>
--------<SubData1>

--------<SubData2> END1 </SubData2>
--------<SubData3> END2 </SubData3>
--<MainXML>

===预期输出=====

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

另外,如何通过匹配文本和逗号将逗号分隔值转换为单独的元素?在这种情况下标记化不起作用吗?因为我想忽略或类似并仅考虑逗号之前的值。 提前致谢!

最佳答案

您的输入 XML 不是有效的 XML(缺少正确的结束标记)。更正后,您可以使用以下XSLT(版本2.0):

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

如果您使用的是 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>

关于java - 将逗号分隔的 xml 值转换为 xml 元素并最后截断不需要的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27212403/

相关文章:

javascript - Jquery-将重复条目解析到单独的列中

java - 如何在 Eclipse 中为不同的项目管理不同的 Maven 设置?

java - Android ClassNotFoundException : Didn't find class on path

linux - Linux下Eclipse在哪里找eclipse.ini

java - 关于 OOP 的问题

java - 作为二叉树的表达式树

Java Array Index out of Bounds 异常 with/for 循环

android - 使用 XML 中的 "animateLayoutChanges"属性对布局进行动画处理

java - 什么是平等契约(Contract)的重要字段(有效的 java 项目 8)

javascript - 返回 XML 节点值