Home > OS >  Getting the first and the last element of a list- using xslt to transform xml in xml
Getting the first and the last element of a list- using xslt to transform xml in xml

Time:04-01

I am sorry my first post was incomplete

Here are some information, it can give an idea of the whole document structure which is very long :

<archdesc id="a0115465927916r4O7Y" altrender="" level="fonds">
    <did>
        <unittitle>Recensements de population 1891-1936</unittitle>
        <unitdate normal="1891-01-01/1936-12-31" type="inclusive">1891-1936</unitdate>
    </did>
    <dsc type="in-depth">


        <c id="a011546592791PZ4nSP" level="item">
            <did>
                <unittitle>ACLOU 1896</unittitle>
                <unitid identifier="6M/192">6M192</unitid>
                <unitdate normal="1896-01-01/1896-12-31" type="inclusive">1896</unitdate>
            </did>
            <daogrp>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0001.jpg"/>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0002.jpg"/>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0003.jpg"/>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0004.jpg"/>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0005.jpg"/>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0006.jpg"/>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0007.jpg"/>
                <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0008.jpg"/>

                It gives an idea of the complete document which is very long. 

I would like to have it just as it looks like, getting all the tags and attributes but changing the pictures lists (daogroup) so as to get the first and last element of each daogroup list.

In this case : <daogrp> <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0001.jpg"/><daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0008.jpg"/></daogrp></c></dsc><:archdesc>

CodePudding user response:

Given your input example:

XML

<archdesc id="a0115465927916r4O7Y" altrender="" level="fonds">
  <did>
    <unittitle>Recensements de population 1891-1936</unittitle>
    <unitdate normal="1891-01-01/1936-12-31" type="inclusive">1891-1936</unitdate>
  </did>
  <dsc type="in-depth">
    <c id="a011546592791PZ4nSP" level="item">
      <did>
        <unittitle>ACLOU 1896</unittitle>
        <unitid identifier="6M/192">6M192</unitid>
        <unitdate normal="1896-01-01/1896-12-31" type="inclusive">1896</unitdate>
      </did>
      <daogrp>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0001.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0002.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0003.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0004.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0005.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0006.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0007.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0008.jpg"/>
      </daogrp>
    </c>
  </dsc>
</archdesc>

the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="daogrp">
    <xsl:copy>
        <xsl:copy-of select="daoloc[1] | daoloc[last()]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

will return:

Result

<?xml version="1.0" encoding="UTF-8"?>
<archdesc id="a0115465927916r4O7Y" altrender="" level="fonds">
  <did>
    <unittitle>Recensements de population 1891-1936</unittitle>
    <unitdate normal="1891-01-01/1936-12-31" type="inclusive">1891-1936</unitdate>
  </did>
  <dsc type="in-depth">
    <c id="a011546592791PZ4nSP" level="item">
      <did>
        <unittitle>ACLOU 1896</unittitle>
        <unitid identifier="6M/192">6M192</unitid>
        <unitdate normal="1896-01-01/1896-12-31" type="inclusive">1896</unitdate>
      </did>
      <daogrp>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0001.jpg"/>
        <daoloc href="recensements/6M192/1896/ACLOU/RP_ACLOU_1896_6M192_0008.jpg"/>
      </daogrp>
    </c>
  </dsc>
</archdesc>

which I believe meets your stated requirement of:

getting all the tags [elements] and attributes but changing the pictures lists (daogroup) so as to get the first and last element of each daogroup list.

CodePudding user response:

as you use the identity transformation template, all nodes/attributes are copied by default, so you can exclude the daoloc elements that don't match.

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
    </xsl:template>

    <!-- suppress unwanted daoloc elements -->
    <xsl:template match="daoloc[not(position()=1) and not(position()=last())]"/>
</xsl:stylesheet>

The expression can be


<xsl:template match="daoloc[position() &gt; 1 and position() &lt; last()]"/>

  • Related