I'm using xslt 1.0. Please help me how to write xslt. I have XML.
<xml name="wwwqqq">
<group key="attribute">
<attribute id="1" name="AA"/>
<attribute id="2" name="BB"/>
<attribute id="3" name="СС"/>
</group>
<group key="tie">
<tie id="1"/>
<tie id="2"/>
</group>
<group key="job">
<job id="1" fromTieId="1" toAttributeId="1" job="bbbbb"/>
<job id="2" fromTieId="1" toAttributeId="2" job="aaaaa"/>
<job id="3" fromTieId="2" toAttributeId="2" job="aaaaa"/>
<job id="4" fromTieId="2" toAttributeId="3" job="ссссс"/>
</group>
I wrote XSLT.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:key name="k_job" match="attribute" use="@id" />
<xsl:template match="/">
<xsl:for-each select="xml/group[@key='job']/job">
<xsl:apply-templates select="key('k_job', @toAttributeId)/@name" />
<xsl:text>_</xsl:text>
<xsl:value-of select="@job"/>
<xsl:text> @fromTieId=</xsl:text>
<xsl:value-of select="@fromTieId"/>
<br />
</xsl:for-each>
</xsl:template>
I need that instead of output
AA_bbbbb @fromTieId=1
BB_aaaaa @fromTieId=1
BB_aaaaa @fromTieId=2
СС_ссссс @fromTieId=2
get
AA_bbbbb_ BB_aaaaa
BB_aaaaa_ СС_ссссс
So you want to group the job
elements first by the fromTieId
attribute? Use Muenchian grouping with <xsl:key name="job-group" match="job" use="@fromTieId"/>
for that and then
<xsl:for-each select="xml/group[@key='job']/job[generate-id() = generate-id(key('job-group', @fromTieId)[1])]">
<xsl:apply-templates select="key('k_job', key('job-group', @fromTieId)/@toAttributeId)/@name" />
For a complete refactoring using templates (but the same keys for grouping/referencing), I think the following approach helps:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="html" indent="yes" version="5.0" doctype-system="about:legacy-doctype" />
<xsl:key name="k_job" match="attribute" use="@id" />
<xsl:key name="job-group" match="job" use="@fromTieId"/>
<xsl:template match="/">
<html>
<head>
<title>Example</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="xml/group[@key='job']/job[generate-id() = generate-id(key('job-group', @fromTieId)[1])]">
<xsl:apply-templates select="key('job-group', @fromTieId)" mode="value"/>
<xsl:text> @fromTieId=</xsl:text>
<xsl:value-of select="@fromTieId"/>
<br />
</xsl:template>
<xsl:template match="xml/group[@key='job']/job[generate-id() != generate-id(key('job-group', @fromTieId)[1])]"/>
<xsl:template match="job" mode="value">
<xsl:apply-templates select="key('k_job', @toAttributeId)/@name" />
<xsl:text>_</xsl:text>
<xsl:value-of select="@job"/>
</xsl:template>
</xsl:stylesheet>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.