xsl-list
[Top] [All Lists]

RE: [xsl] eliminating multiple repeated tags in my XML

2008-03-14 14:30:13
Last week David very nicely helped me with a bit of XSLT, and I now
discover a variation in my XML I need to adjust around.  The XML looks
like this:

<article><front><article-meta>
[...other related-article siblings...]
<related-article related-article-type="original-article">
        <person-group person-group-type="author">
                <name>
                        <surname>Surname1</surname>
                        <given-names>GivenName1</given-names>
                </name>
        </person-group>
</related-article>
<related-article related-article-type="original-article">
        <person-group person-group-type="author">
                <name>
                        <surname>Surname2</surname>
                        <given-names>GivenName2</given-names>
                </name>
        </person-group>
        <article-title>Title</article-title>
        <source>Source</source>
        TEXT CONTENT
        <volume>34</volume>
</related-article>
[...other related-article siblings...]
</article-meta> etc.


with the text content inside <related-article>.  What I am looking to do
again is produce XML that looks like this:


<article><front><article-meta>
[...other related-article siblings...]
<related-article related-article-type="original-article">
        <person-group person-group-type="author">
                <name>
                        <surname>Surname1</surname>
                        <given-names>GivenName1</given-names>
                </name>
        </person-group>
        <person-group person-group-type="author">
                <name>
                        <surname>Surname2</surname>
                        <given-names>GivenName2</given-names>
                </name>
        </person-group>
        <article-title>Title</article-title>
        <source>Source</source>
        TEXT CONTENT
        <volume>34</volume>
</related-article>
[...other related-article siblings...]
</article-meta> etc.


...where the two <related-article>s are now collapsed into one single
<related-article>.  The XSLT that David sent is this:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
    <xsl:template match="article-meta">
        <xsl:copy copy-namespaces="no" inherit-namespaces="no">
            <xsl:copy-of select="@*" copy-namespaces="no"/>
            <xsl:for-each-group select="*"
 
group-adjacent="exists(self::related-article[(_at_)related-article-type='orig
inal-article'])">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <related-article
related-article-type="original-article">
                            <xsl:copy-of select="current-group()/*"
copy-namespaces="no"/>
                        </related-article>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="current-group()/*"
copy-namespaces="no"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy copy-namespaces="no" inherit-namespaces="no">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

which does work, but drops the text content.  My attempts at fixing this
having failed so far, I'd appreciate any suggestions.

Thanks again, all.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--