XSLT解析具有标记ID帮助的多个嵌套子级

时间:2011-08-23 23:36:24

标签: xml xslt global-variables children nested-attributes

我有以下需要使用XSLT解析为名称/值对的xml。无论我尝试什么,我都无法做到。我知道你不能有一个全局的“计数器”来跟踪批次,但我已经尝试过递归模板,调用java助手类,position(),count,你给它命名......我被卡住了。

对于所有包含的子元素,“批次”需要编号相同。

这是我的XML片段:

<extendedDataElements name="Feed" type="string">
<children name="fileId" type="string">
      <values>TXT123</values>
</children>

<children name="processedTime" type="dateTime">
      <values>2011-07-08T20:15:38</values>
</children>

<children name="batch" type="string">
    <values>0000001</values>
    <children name="statusCode" type="string">
        <values>new</values>
    </children>
</children> 

<children name="batch" type="string">
    <values>0000002</values>
    <children name="statusCode" type="string">
        <values>start</values>
    </children>       
</children>

<children name="batch" type="string">
    <values>0000003</values>
    <children name="statusCode" type="string">
        <values>new</values>
     </children>
</children>

<children name="count" type="long">
      <values>26</values>
</children>
<children name="value" type="double">
      <values>83171.650</values>
</children>
</extendedDataElements>

这就是我需要的:

fileID=TXT123, processedTime=2011-07-08T20:15:38, batch1=0000001, statusCode1=new, batch2=0000002, statusCode2=start, batch3=0000003, statusCode3=new, count=26, value=83171.650

2 个答案:

答案 0 :(得分:1)

您可以使用XPath number()函数重命名批次对。例如,XPath number('0000001')=1。这些与XPath concat()一起将帮助您组装所需的批次对。

要处理逗号,我选择了模板模式技术,但您可以根据需要处理它。

[XSLT 1.0]

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:apply-templates select="children"/>
    </xsl:template>

    <xsl:template match="children[not(@name='batch')]">
        <xsl:value-of select="concat(
            @name,'=',values)"/>
        <xsl:apply-templates select="self::*[following-sibling::*]" 
            mode="comma"/>
    </xsl:template>

    <xsl:template match="children[@name='batch']">
        <xsl:value-of select="concat(
            @name,number(values),'=',values,', ',
            children/@name,number(values),'=',children/values)"/>
        <xsl:apply-templates select="self::*[following-sibling::*]" 
            mode="comma"/>
    </xsl:template>

    <xsl:template match="children" mode="comma">
        <xsl:value-of select="', '"/>
    </xsl:template>

</xsl:stylesheet>

带输出:

fileId=TXT123, processedTime=2011-07-08T20:15:38, batch1=0000001, statusCode1=new, batch2=0000002, statusCode2=start, batch3=0000003, statusCode3=new, count=26, value=83171.650

答案 1 :(得分:1)

此转化

<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="children">
         <xsl:if test="not(position()=1)">, </xsl:if>
         <xsl:value-of select="concat(@name,'=',values)"/>
 </xsl:template>

 <xsl:template match="children[@name='batch']">
  <xsl:variable name="vNum">
   <xsl:number count="children[@name='batch']" level="single"/>
  </xsl:variable>

         <xsl:if test="not(position()=1)">, </xsl:if>
  <xsl:value-of select="concat('batch', $vNum, '=', values)"/>

  <xsl:apply-templates select="children"/>
 </xsl:template>

 <xsl:template match=
  "children[@name='batch']/children">

  <xsl:variable name="vNum">
    <xsl:for-each select="..">
      <xsl:number count="children[@name='batch']" level="single"/>
     </xsl:for-each>
  </xsl:variable>

  <xsl:value-of select=
    "concat(', ',@name,$vNum,'=',values)"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<extendedDataElements name="Feed" type="string">
    <children name="fileId" type="string">
        <values>TXT123</values>
    </children>
    <children name="processedTime" type="dateTime">
        <values>2011-07-08T20:15:38</values>
    </children>
    <children name="batch" type="string">
        <values>0000001</values>
        <children name="statusCode" type="string">
            <values>new</values>
        </children>
    </children>
    <children name="batch" type="string">
        <values>0000002</values>
        <children name="statusCode" type="string">
            <values>start</values>
        </children>
    </children>
    <children name="batch" type="string">
        <values>0000003</values>
        <children name="statusCode" type="string">
            <values>new</values>
        </children>
    </children>
    <children name="count" type="long">
        <values>26</values>
    </children>
    <children name="value" type="double">
        <values>83171.650</values>
    </children>
</extendedDataElements>

生成想要的正确结果

fileId=TXT123, processedTime=2011-07-08T20:15:38, batch1=0000001, statusCode1=new, batch2=0000002, statusCode2=start, batch3=0000003, statusCode3=new, count=26, value=83171.650

解释:充分利用以下基本XSLT功能:

  1. 模板和模式匹配

  2. <强> <xsl:number>