XSLT分组但忽略空属性

时间:2016-09-08 23:01:46

标签: xslt

我有一个XSLT可以将XML输入转换为JSON,但是当其中一个group-by具有空值时,我遇到了问题。简而言之,我转换一个键/值对并将数据转出,这样我就可以将它插入到数据库中。我的问题是@TableName在某些情况下是空的(这会导致JSON输出中出现错误的逗号)并且我需要绕过它们,问题是我无法获得position()和last()因为它们没有&#39 ; t包含准确的值,因为TableName为空。我尝试过使用group-starting-with,但我无法正确使用语法。我愿意接受建议。示例XML输入到XSLT

<root xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <Metric measType="1526727077"
           measResult="0"
           endTime="2016-08-25T04:30:00-07:00"
           measObjLdn="LTHBC0126858/HANS:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7"
           Element_Type="ENODEB"
           Key1="LTHBC0126858"
           TableName=""
           ColumnName=""/>
   <Metric measType="1526727075"
           measResult="0"
           endTime="2016-08-25T04:30:00-07:00"
           measObjLdn="LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7"
           Element_Type="ENODEB"
           Key1="LTHBC0126858"
           TableName="HH_ENODEB"
           ColumnName="H1526727075"
           H1526727075="0"/>
   <Metric measType="1526727076"
           measResult="0"
           endTime="2016-08-25T04:30:00-07:00"
           measObjLdn="LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7"
           Element_Type="ENODEB"
           Key1="LTHBC0126858"
           TableName="HH_ENODEB"
           ColumnName="H1526727076"
           H1526727076="0"/>
   <Metric measType="1526727077"
           measResult="0"
           endTime="2016-08-25T04:30:00-07:00"
           measObjLdn="LTHBC0126858/DIFF:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7"
           Element_Type="ENODEB"
           Key1="LTHBC0126858"
           TableName="DIFF"
           ColumnName="H1526727077"
           H1526727077="0"/>
   <Metric measType="1526737817"
           measResult="0"
           endTime="2016-08-25T04:30:00-07:00"
           measObjLdn="LTHBC0126858/Board.Service:Board Type=BBP, Cabinet No.=0, Subrack No.=1, Slot No.=3"
           Element_Type="Unknown"
           Key1="Unknown"
           TableName=""/>
</root>

我的XSLT中有一些伪造的<TEXT>输出用于调试目的

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:output method="text" encoding="utf-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="root">
        <xsl:text>{"root":{</xsl:text>
        <xsl:text>"Table":[</xsl:text>
        <xsl:for-each-group select="Metric" group-by="@measObjLdn">
            <xsl:sort select="current-grouping-key()"/>
            <xsl:variable name="curr_key" select="current-grouping-key()"/>
            <xsl:for-each-group select="current-group()" group-by="@TableName">
                <xsl:sort select="current-grouping-key()"/>
                <xsl:variable name="curr_table" select="current-grouping-key()"/>
            <xsl:text>Table</xsl:text>
            <xsl:value-of select="$curr_table"/>
                <xsl:if test="current-grouping-key() != ''">
                    <xsl:text>{"TableName":"</xsl:text>
                    <xsl:value-of select="current-grouping-key()"/>
                    <xsl:text>",</xsl:text>
                    <xsl:text>"Stamp":"</xsl:text>
                    <xsl:value-of select="@endTime"/>
                    <xsl:text>",</xsl:text>
                    <xsl:text>"measObjLdn":"</xsl:text>
                    <xsl:value-of select="$curr_key"/>
                    <xsl:text>",</xsl:text>
                    <xsl:text>"Element_Type":"</xsl:text>
                    <xsl:value-of select="@Element_Type"/>
                    <xsl:text>",</xsl:text>
                    <xsl:text>"Key1":"</xsl:text>
                    <xsl:value-of select="@Key1"/>
                    <xsl:text>"</xsl:text>
                    <xsl:for-each select="current-group()">
                        <xsl:for-each select="@*">
                            <xsl:if test="starts-with(name(), 'H')">
                                <xsl:text>,"</xsl:text>
                                <xsl:value-of select="name()"/>
                                <xsl:text>":"</xsl:text>
                                <xsl:value-of select="number(.)"/>
                                <xsl:text>"</xsl:text>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:for-each>
                    <xsl:text>}</xsl:text>
                </xsl:if>
            </xsl:for-each-group>
            <xsl:text>position</xsl:text>
            <xsl:value-of select="position()"/>
            <xsl:text>last</xsl:text>
            <xsl:value-of select="last()"/>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each-group>
        <xsl:text>]}}</xsl:text>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

在这种情况下你应该做的是对xsl:for-each-group的空表名进行条件检查,如下所示:

<xsl:for-each-group select="current-group()[@TableName!='']" group-by="@TableName">
然后

position()将按预期工作,它指的是所选节点集中节点的位置,而不是实际XML层次结构中的位置。

此外,您应该以类似的方式更改xsl:for-each-group元素Metric

<xsl:for-each-group select="Metric[@TableName != '']" group-by="@measObjLdn">

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:output method="text" encoding="utf-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="root">
        <xsl:text>{"root":{</xsl:text>
        <xsl:text>"Table":[</xsl:text>
        <xsl:for-each-group select="Metric[@TableName != '']" group-by="@measObjLdn">
            <xsl:sort select="current-grouping-key()"/>
            <xsl:variable name="curr_key" select="current-grouping-key()"/>
            <xsl:for-each-group select="current-group()[@TableName!='']" group-by="@TableName">
                <xsl:sort select="current-grouping-key()"/>
                <xsl:variable name="curr_table" select="current-grouping-key()"/>
                <xsl:text>{"TableName":"</xsl:text>
                <xsl:value-of select="current-grouping-key()"/>
                <xsl:text>",</xsl:text>
                <xsl:text>"Stamp":"</xsl:text>
                <xsl:value-of select="@endTime"/>
                <xsl:text>",</xsl:text>
                <xsl:text>"measObjLdn":"</xsl:text>
                <xsl:value-of select="$curr_key"/>
                <xsl:text>",</xsl:text>
                <xsl:text>"Element_Type":"</xsl:text>
                <xsl:value-of select="@Element_Type"/>
                <xsl:text>",</xsl:text>
                <xsl:text>"Key1":"</xsl:text>
                <xsl:value-of select="@Key1"/>
                <xsl:text>"</xsl:text>
                <xsl:for-each select="current-group()">
                    <xsl:for-each select="@*">
                        <xsl:if test="starts-with(name(), 'H')">
                            <xsl:text>,"</xsl:text>
                            <xsl:value-of select="name()"/>
                            <xsl:text>":"</xsl:text>
                            <xsl:value-of select="number(.)"/>
                            <xsl:text>"</xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:for-each>
                <xsl:text>}</xsl:text>
            </xsl:for-each-group>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each-group>
        <xsl:text>]}}</xsl:text>
    </xsl:template>
</xsl:stylesheet>

这会生成以下JSON

{
   "root":{
      "Table":[
         {
            "TableName":"DIFF",
            "Stamp":"2016-08-25T04:30:00-07:00",
            "measObjLdn":"LTHBC0126858/DIFF:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7",
            "Element_Type":"ENODEB",
            "Key1":"LTHBC0126858",
            "H1526727077":"0"
         },
         {
            "TableName":"HH_ENODEB",
            "Stamp":"2016-08-25T04:30:00-07:00",
            "measObjLdn":"LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7",
            "Element_Type":"ENODEB",
            "Key1":"LTHBC0126858",
            "H1526727075":"0",
            "H1526727076":"0"
         }
      ]
   }
}