XML-XSLT元素列表-具有相同属性值的连接元素 [英] XML - XSLT List of elements - Joining elements with same attribute value
问题描述
我正在尝试生成具有特定结构的XML文件,但是我被卡住了,
I'm trying to produce a XML file with a certain structure but I'm stuck,
这是我输入的XML文件:
This is my input XML file:
<report>
<page name="Simple MasterPage">
<table id="__bookmark_1">
<table-band band-type="BAND_HEADER">
<row>
<cell>
<label>PERSONNAME</label>
</cell>
<cell>
<label>PERSONID</label>
</cell>
<cell>
<label>NUMELEM</label>
</cell>
<cell>
<label>CREATIONDATE</label>
</cell>
<cell>
<label>CREATIONDATE</label>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>1</data>
</cell>
<cell>
<data>2018-06-21</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>2</data>
</cell>
<cell>
<data>2018-06-21</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>3</data>
</cell>
<cell>
<data>2018-06-21</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>4</data>
</cell>
<cell>
<data>2018-06-21</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>5</data>
</cell>
<cell>
<data>2018-06-21</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>Marie</data>
</cell>
<cell>
<data>p2</data>
</cell>
<cell>
<data>6</data>
</cell>
<cell>
<data>2018-06-21</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>Marie</data>
</cell>
<cell>
<data>p2</data>
</cell>
<cell>
<data>7</data>
</cell>
<cell>
<data>2018-06-21</data>
</cell>
</row>
</table-band>
</table>
</page>
</report>
这是我用来转换它的XSLT,该用户@Parfait教我如何在一个非常相似的帖子上进行构建:
This is the XSLT I'm using to transform it, that user @Parfait teached me how to build on a very similar post:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" omit-xml-declaration="no" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/report/page/table">
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versao="1.0">
<!--Para comecar da 2 tabela-->
<xsl:apply-templates select="table-band[position() > 1] "/>
</entry>
</xsl:template>
<xsl:template name="top_build-attrib">
<xsl:param name="label_val"/>
<xsl:param name="attrib_nm"/>
<xsl:variable name="row_num" select="count(table-band[1]/row/cell[label=$label_val]/preceding-sibling::*)+1"/>
<xsl:attribute name="{$attrib_nm}"><xsl:value-of select="table-band[2]/row/cell[position()=$row_num]/data"/></xsl:attribute>
</xsl:template>
<xsl:template name="build-attrib">
<xsl:param name="label_val"/>
<xsl:param name="attrib_nm"/>
<xsl:variable name="row_num" select="count(ancestor::table/table-band[1]/row/cell[label=$label_val]/preceding-sibling::*)+1"/>
<xsl:attribute name="{$attrib_nm}"><xsl:value-of select="row/cell[position()=$row_num]/data"/></xsl:attribute>
</xsl:template>
<xsl:template name="elem_value">
<xsl:param name="label_val"/>
<xsl:variable name="row_num" select="count(ancestor::table/table-band[1]/row/cell[label=$label_val]/preceding-sibling::*)+1"/>
<xsl:value-of select="row/cell[position()=$row_num]/data"/>
</xsl:template>
<xsl:template match="table-band">
<person>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">PERSONNAME</xsl:with-param>
<xsl:with-param name="attrib_nm">personName</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">PERSONID</xsl:with-param>
<xsl:with-param name="attrib_nm">personID</xsl:with-param>
</xsl:call-template>
<listOfElements>
<element>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">NUMELEM</xsl:with-param>
<xsl:with-param name="attrib_nm">numElem</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">CREATIONDATE</xsl:with-param>
<xsl:with-param name="attrib_nm">creationDate</xsl:with-param>
</xsl:call-template>
</element>
</listOfElements>
</person>
</xsl:template>
</xsl:stylesheet>
这就是我得到的结果:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versao="1.0">
<person personID="p1" personName="John">
<listOfElements>
<element creationDate="2018-06-21" numElem="1"/>
</listOfElements>
</person>
<person personID="p1" personName="John">
<listOfElements>
<element creationDate="2018-06-21" numElem="2"/>
</listOfElements>
</person>
<person personID="p1" personName="John">
<listOfElements>
<element creationDate="2018-06-21" numElem="3"/>
</listOfElements>
</person>
<person personID="p1" personName="John">
<listOfElements>
<element creationDate="2018-06-21" numElem="4"/>
</listOfElements>
</person>
<person personID="p1" personName="John">
<listOfElements>
<element creationDate="2018-06-21" numElem="5"/>
</listOfElements>
</person>
<person personID="p2" personName="Marie">
<listOfElements>
<element creationDate="2018-06-21" numElem="6"/>
</listOfElements>
</person>
<person personID="p2" personName="Marie">
<listOfElements>
<element creationDate="2018-06-21" numElem="7"/>
</listOfElements>
</person>
</entry>
但是我要寻找的结果是要做到这一点:
But the result I'm looking for and that am required to prodocue is this one:
<?xml version="1.0" encoding="utf-8"?>
<entry>
<person personID="p1" personName="John">
<listOfElements>
<element creationDate="2018-06-21" numElem="1" />
<element creationDate="2018-06-21" numElem="2" />
<element creationDate="2018-06-21" numElem="3" />
<element creationDate="2018-06-21" numElem="4" />
<element creationDate="2018-06-21" numElem="5"/>
</listOfElements>
</person>
<person personID="p2" personName="Marie">
<listOfElements>
<element creationDate="2018-06-21" numElem="6" />
<element creationDate="2018-06-21" numElem="7" />
</listOfElements>
</person>
</entry>
意味着我无法像我应该的那样生成listOfElements
,我正在打印一个新的person
节点,而不是加入"同一person
节点中的element
节点(在listOfElements
节点内)
Meaning that the I'm not being able to produce the listOfElements
like I should, I'm printing a new person
node instead of "joining" the element
nodes in the same person
node (inside the listOfElements
node)
我知道解决方案必须与比较presonID
或personName
属性有关,但是我只是没有到达...
I know that the solution must have something to do with comparing the presonID
or personName
attributes, but I'm just not getting there...
我真的需要一些帮助人员,谢谢!
I really need some help guys, thank you!
编辑
我尝试在每个element
标记之前添加xsl:for-each-group
,但仍然得到相同的结果.请执行以下操作:
I tried addin the xsl:for-each-group
before each element
tag but I still get the same result. Did the following:
<listOfElements>
<xsl:for-each-group select="table-band[@band-type='BAND_DETAIL']/row" group-by="cell[2]/data">
<element>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">NUMELEM</xsl:with-param>
<xsl:with-param name="attrib_nm">numElem</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">CREATIONDATE</xsl:with-param>
<xsl:with-param name="attrib_nm">creationDate</xsl:with-param>
</xsl:call-template>
</element>
</xsl:for-each-group>
</listOfElements>
编辑 我更改了输入xml和输出,这意味着输入xml丢失了NUMDOC和CREATIONDATE元素,并获得了新的元素ELEMENTTYPE
EDIT I changed the input xml and output, meaning that the input xml lost the NUMDOC and CREATIONDATE elements, and gained a new one, ELEMENTTYPE,
因此,我失去了在内部for-each-group
中使用的分组因子,即NUMDOC
,就像@Parfait教给我的那样.现在,我不知道如何仅使用ELEMENTTYPE
来对元素进行分组.请注意,我希望对于同一个人,具有相同ELEMENTTYPE
的不同元素,所以这不能用作for-each-group
中的因子,或者可以吗?
So, I lost the grouping factor I used in the inner for-each-group
, that was NUMDOC
, like @Parfait teached me. Now I don't know how to group the elements with only having the ELEMENTTYPE
. Note that I want to be able to have, for the same person, different elements with the same ELEMENTTYPE
, so this can't be use for the factor in the for-each-group
, or can it?
输入XML :(已更新)
INPUT XML:(UPDATED)
<report>
<page name="Simple MasterPage">
<table id="__bookmark_1">
<table-band band-type="BAND_HEADER">
<row>
<cell>
<label>PERSONNAME</label>
</cell>
<cell>
<label>PERSONID</label>
</cell>
<cell>
<label>ELEMENTTYPE</label>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>001</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>001</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>002</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>001</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>John</data>
</cell>
<cell>
<data>p1</data>
</cell>
<cell>
<data>002</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>Marie</data>
</cell>
<cell>
<data>p2</data>
</cell>
<cell>
<data>001</data>
</cell>
</row>
</table-band>
<table-band band-type="BAND_DETAIL">
<row>
<cell>
<data>Marie</data>
</cell>
<cell>
<data>p2</data>
</cell>
<cell>
<data>001</data>
</cell>
</row>
</table-band>
</table>
</page>
</report>
所以我想得到的输出是:
So the output I wanna get is:
<?xml version="1.0" encoding="utf-8"?>
<entry>
<person personID="p1" personName="John">
<listOfElements>
<element elementType="001" />
<element elementType="001" />
<element elementType="002" />
<element elementType="001" />
<element elementType="002" />
</listOfElements>
</person>
<person personID="p2" personName="Marie">
<listOfElements>
<element elementType="001" />
<element elementType="001" />
</listOfElements>
</person>
</entry>
我不知道在内部for-each-group
中使用什么,就像@Parfait告诉我的那样,我使用了NUMDOC
,但是现在我没有什么可区分文档了!
I don't know what to use in the inner for-each-group
, before , like @Parfait showed me, I used NUMDOC
, but now I don't have anything to distinguish between docs!
谢谢!
Alexandre Jacinto
Alexandre Jacinto
推荐答案
考虑在当前XSLT的最后一个模板中应用两层的<xsl:for-each-group>
,指向 PERSONNAME 和 NUMELEM 在第一个<table-band>
中的对应位置:
Consider applyling a two-layered <xsl:for-each-group>
in your last template of current XSLT, pointing to the PERSONNAME and NUMELEM corresponding position in first <table-band>
:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/report/page">
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versao="1.0">
<!--Para comecar da 2 tabela-->
<xsl:apply-templates select="table"/>
</entry>
</xsl:template>
<xsl:template name="top_build-attrib">
<xsl:param name="label_val"/>
<xsl:param name="attrib_nm"/>
<xsl:variable name="row_num" select="count(table-band[1]/row/cell[label=$label_val]/preceding-sibling::*)+1"/>
<xsl:attribute name="{$attrib_nm}"><xsl:value-of select="table-band[2]/row/cell[position()=$row_num]/data"/></xsl:attribute>
</xsl:template>
<xsl:template name="build-attrib">
<xsl:param name="label_val"/>
<xsl:param name="attrib_nm"/>
<xsl:variable name="row_num" select="count(ancestor::table/table-band[1]/row/cell[label=$label_val]/preceding-sibling::*)+1"/>
<xsl:attribute name="{$attrib_nm}"><xsl:value-of select="row/cell[position()=$row_num]/data"/></xsl:attribute>
</xsl:template>
<xsl:template name="elem_value">
<xsl:param name="label_val"/>
<xsl:variable name="row_num" select="count(ancestor::table/table-band[1]/row/cell[label=$label_val]/preceding-sibling::*)+1"/>
<xsl:value-of select="row/cell[position()=$row_num]/data"/>
</xsl:template>
<xsl:template match="table">
<xsl:for-each-group select="table-band" group-by="row/cell[position()=
count(ancestor::table/table-band[1]/row/cell[label='PERSONNAME']/preceding-sibling::*)+1]/data">
<person>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">PERSONID</xsl:with-param>
<xsl:with-param name="attrib_nm">personID</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">PERSONNAME</xsl:with-param>
<xsl:with-param name="attrib_nm">personName</xsl:with-param>
</xsl:call-template>
<listOfElements>
<xsl:for-each-group select="current-group()" group-by="row/cell[position()=
count(ancestor::table/table-band[1]/row/cell[label='NUMELEM']/preceding-sibling::*)+1]">
<element>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">NUMELEM</xsl:with-param>
<xsl:with-param name="attrib_nm">numElem</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="build-attrib">
<xsl:with-param name="label_val">CREATIONDATE</xsl:with-param>
<xsl:with-param name="attrib_nm">creationDate</xsl:with-param>
</xsl:call-template>
</element>
</xsl:for-each-group>
</listOfElements>
</person>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
这篇关于XML-XSLT元素列表-具有相同属性值的连接元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!