获取列表的第一个和最后一个元素 - 使用 xslt 将 xml 转换为 xml

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

很抱歉我的第一个 post 不完整

这里有一些信息,可以大致了解整个很长的文档结构:

<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. 

我想要它看起来的样子,获取所有标签和属性,但更改图片列表 (daogroup),以便获取每个 daogroup 列表的第一个和最后一个元素。

在这种情况下:<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>

鉴于您的输入示例:

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>

以下样式表:

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>

将return:

结果

<?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>

我认为这符合您规定的要求:

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.

由于您使用身份转换模板,默认情况下会复制所有 nodes/attributes,因此您可以排除不匹配的 daoloc 元素。

<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>

表达式可以是


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