Это XSLT 1.0 преобразование
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="utf-8" />
<xsl:key name="kRecordG2" match="Record" use="
concat(
FieldValue[@fieldName='formulierid']/@fieldValue,
':',
FieldValue[@fieldName='value']/@fieldValue
)
" />
<xsl:template match="Report">
<bestand registratienummer="93391"><!-- the number is nowhere in the input -->
<xsl:apply-templates mode="rapportage" select="
Record[
generate-id()
=
generate-id(key('kRecordG2',
concat(
FieldValue[@fieldName='formulierid']/@fieldValue,
':',
FieldValue[@fieldName='value']/@fieldValue
)
)[1])
]
" />
</bestand>
</xsl:template>
<xsl:template match="Record" mode="rapportage">
<rapportage>
<xsl:apply-templates select="FieldValue[@fieldName='rapportage_nihil']" />
<xsl:apply-templates select="FieldValue[@fieldName='periode']" />
<xsl:apply-templates select="FieldValue[@fieldName='formulierid']" />
<xsl:apply-templates select="FieldValue[@fieldName='versie']" />
<xsl:apply-templates select="FieldValue[@fieldName='frequentie']" />
<variant type="Landen" value="{FieldValue[@fieldName='value']/@fieldValue}"/>
<xsl:apply-templates mode="post" select="
key('kRecordG2',
concat(
FieldValue[@fieldName='formulierid']/@fieldValue,
':',
FieldValue[@fieldName='value']/@fieldValue
)
)
" />
</rapportage>
</xsl:template>
<xsl:template match="Record" mode="post">
<post>
<xsl:apply-templates select="FieldValue[@fieldName='post_value']" />
<xsl:apply-templates select="FieldValue[@fieldName='cube']" />
<xsl:apply-templates select="FieldValue[@fieldName='rij']" />
<xsl:apply-templates select="FieldValue[@fieldName='kolom']" />
</post>
</xsl:template>
<xsl:template match="FieldValue">
<xsl:variable name="attrname">
<xsl:choose>
<xsl:when test="@fieldName = 'rapportage_nihil'">nihil</xsl:when>
<xsl:when test="@fieldName = 'post_value'">value</xsl:when>
<xsl:otherwise><xsl:value-of select="@fieldName" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:attribute name="{$attrname}">
<xsl:value-of select="@fieldValue" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
дает:
<bestand registratienummer="93391">
<rapportage nihil="false" periode="31-Mar-2010" formulierid="9001HK4V10" versie="1" frequentie="M">
<variant type="Landen" value="0003" />
<post value="61,941.00" cube="c01" kolom="c_2250_SPU"></post>
<post value="2.00" cube="c01" kolom="c_2250_SPU"></post>
</rapportage>
<rapportage nihil="false" periode="31-Mar-2010" formulierid="9001HK1V10" versie="1" frequentie="M">
<variant type="Landen" value="0003" />
<post value="4,157.00" cube="c01" kolom="c_2250_SPU"></post>
<post value="61.00" cube="c01" kolom="c_2242_SPU"></post>
</rapportage>
<rapportage nihil="false" periode="31-Mar-2010" formulierid="9001HK1V10" versie="1" frequentie="M">
<variant type="Landen" value="0629" />
<post value="1.00" cube="c01" kolom="c_2250_SPU"></post>
</rapportage>
<rapportage nihil="false" periode="31-Mar-2010" formulierid="9001HK1V10" versie="1" frequentie="M">
<variant type="Landen" value="0002" />
<post value="3.00" cube="c01" kolom="c_9000_SPU"></post>
</rapportage>
</bestand>
Я работаю над более элегантной версией, посмотрим, смогу ли я найти ее ...
Ну, я думаю, что это не намного лучше, по крайней мере, в XSLT 1.0. Возможно, немного быстрее для больших наборов ввода, используя дополнительную клавишу:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="utf-8" />
<xsl:key name="kRecordG2" match="Record" use="
concat(
FieldValue[@fieldName='formulierid']/@fieldValue,
':',
FieldValue[@fieldName='value']/@fieldValue
)
" />
<xsl:key name="kProp" match="FieldValue" use="
concat(generate-id(..), @fieldName)
" />
<xsl:template match="Report">
<bestand registratienummer="93391"><!-- the number is nowhere in the input -->
<xsl:apply-templates mode="rapportage" select="
Record[
generate-id()
=
generate-id(key('kRecordG2',
concat(
FieldValue[@fieldName='formulierid']/@fieldValue,
':',
FieldValue[@fieldName='value']/@fieldValue
)
)[1])
]
" />
</bestand>
</xsl:template>
<xsl:template match="Record" mode="rapportage">
<rapportage>
<xsl:variable name="id" select="generate-id()" />
<xsl:apply-templates select="key('kProp', concat($id, 'rapportage_nihil'))" />
<xsl:apply-templates select="key('kProp', concat($id, 'periode'))" />
<xsl:apply-templates select="key('kProp', concat($id, 'formulierid'))" />
<xsl:apply-templates select="key('kProp', concat($id, 'versie'))" />
<xsl:apply-templates select="key('kProp', concat($id, 'frequentie'))" />
<variant type="Landen" value="{key('kProp', concat($id, 'value'))/@fieldValue}"/>
<xsl:apply-templates mode="post" select="
key('kRecordG2',
concat(
key('kProp', concat($id, 'formulierid'))/@fieldValue,
':',
key('kProp', concat($id, 'value'))/@fieldValue
)
)
" />
</rapportage>
</xsl:template>
<xsl:template match="Record" mode="post">
<post>
<xsl:variable name="id" select="generate-id()" />
<xsl:apply-templates select="key('kProp', concat($id, 'post_value'))" />
<xsl:apply-templates select="key('kProp', concat($id, 'cube'))" />
<xsl:apply-templates select="key('kProp', concat($id, 'cube'))" />
<xsl:apply-templates select="key('kProp', concat($id, 'kolom'))" />
</post>
</xsl:template>
<xsl:template match="FieldValue">
<xsl:variable name="attrname">
<xsl:choose>
<xsl:when test="@fieldName = 'rapportage_nihil'">nihil</xsl:when>
<xsl:when test="@fieldName = 'post_value'">value</xsl:when>
<xsl:otherwise><xsl:value-of select="@fieldName" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:attribute name="{$attrname}">
<xsl:value-of select="@fieldValue" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>