需要转换 XML 以获得 XSLT 1.0 中的唯一代码
Need to convert XML for unique code in XSLT 1.0
我从其中一个系统中获得了以下格式的 XML。我需要转换 XML.
<parentTag>
<childTag>
<date>28-Jan-2017</date>
<code>DoB</code>
<oldStatus>g</oldStatus>
<newStatus>22</newStatus>
</childTag>
<childTag>
<date>27-Jan-2017</date>
<code>www</code>
<oldStatus>25</oldStatus>
<newStatus>g</newStatus>
</childTag>
<childTag>
<date>26-Jan-2017</date>
<code>DoB</code>
<oldStatus>56</oldStatus>
<newStatus>73</newStatus>
</childTag>
<childTag>
<date>26-Jan-2017</date>
<code>www</code>
<oldStatus>66</oldStatus>
<newStatus>55</newStatus>
</childTag>
</parentTag>
我需要收集每个代码的所有详细信息并使用 XSLT 1.0 如下所示转换 XML。
<parentTag>
<childTag>
<code>DoB</code>
<status>
<date>28-Jan-2017</date>
<oldStatus>g</oldStatus>
<newStatus>22</newStatus>
</status>
<status>
<date>26-Jan-2017</date>
<oldStatus>56</oldStatus>
<newStatus>73</newStatus>
</status>
</childTag>
<childTag>
<code>www</code>
<status>
<date>27-Jan-2017</date>
<oldStatus>25</oldStatus>
<newStatus>g</newStatus>
</status>
<status>
<date>26-Jan-2017</date>
<oldStatus>66</oldStatus>
<newStatus>55</newStatus>
</status>
</childTag>
</parentTag>
我请你帮忙解决这个问题。
谢谢。
您可以在 xslt 1.0
中使用 muenchian 分组
<?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"
exclude-result-prefixes="xs"
version="1.0">
<xsl:key name="keycode" match="childTag" use="code"/>
<xsl:template match="parentTag">
<xsl:copy>
<xsl:for-each select="childTag[generate-id() = generate-id(key('keycode', code)[1])]">
<childTag>
<xsl:copy-of select="code"/>
<xsl:variable name="currentcode" select="normalize-space(code)"/>
<xsl:for-each select="key('keycode', $currentcode)">
<status>
<xsl:copy-of select="date|oldStatus|newStatus"/>
</status>
</xsl:for-each>
</childTag>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我从其中一个系统中获得了以下格式的 XML。我需要转换 XML.
<parentTag>
<childTag>
<date>28-Jan-2017</date>
<code>DoB</code>
<oldStatus>g</oldStatus>
<newStatus>22</newStatus>
</childTag>
<childTag>
<date>27-Jan-2017</date>
<code>www</code>
<oldStatus>25</oldStatus>
<newStatus>g</newStatus>
</childTag>
<childTag>
<date>26-Jan-2017</date>
<code>DoB</code>
<oldStatus>56</oldStatus>
<newStatus>73</newStatus>
</childTag>
<childTag>
<date>26-Jan-2017</date>
<code>www</code>
<oldStatus>66</oldStatus>
<newStatus>55</newStatus>
</childTag>
</parentTag>
我需要收集每个代码的所有详细信息并使用 XSLT 1.0 如下所示转换 XML。
<parentTag>
<childTag>
<code>DoB</code>
<status>
<date>28-Jan-2017</date>
<oldStatus>g</oldStatus>
<newStatus>22</newStatus>
</status>
<status>
<date>26-Jan-2017</date>
<oldStatus>56</oldStatus>
<newStatus>73</newStatus>
</status>
</childTag>
<childTag>
<code>www</code>
<status>
<date>27-Jan-2017</date>
<oldStatus>25</oldStatus>
<newStatus>g</newStatus>
</status>
<status>
<date>26-Jan-2017</date>
<oldStatus>66</oldStatus>
<newStatus>55</newStatus>
</status>
</childTag>
</parentTag>
我请你帮忙解决这个问题。
谢谢。
您可以在 xslt 1.0
中使用 muenchian 分组<?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"
exclude-result-prefixes="xs"
version="1.0">
<xsl:key name="keycode" match="childTag" use="code"/>
<xsl:template match="parentTag">
<xsl:copy>
<xsl:for-each select="childTag[generate-id() = generate-id(key('keycode', code)[1])]">
<childTag>
<xsl:copy-of select="code"/>
<xsl:variable name="currentcode" select="normalize-space(code)"/>
<xsl:for-each select="key('keycode', $currentcode)">
<status>
<xsl:copy-of select="date|oldStatus|newStatus"/>
</status>
</xsl:for-each>
</childTag>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>