我有这样的XML

    <mets:mets xmlns:mets="http://www.loc.gov/METS/">
  <mets:fileSec>
    <mets:fileGrp ID="REP1">
      <mets:file ID="FL1">
        <mets:FLocat LOCTYPE="URL" xlin:href="1.jpg" xmlns:xlin="http://www.w3.org/1999/xlink"/>
      </mets:file>
    </mets:fileGrp>
    <mets:fileGrp ID="REP2">
      <mets:file ID="FL2">
        <mets:FLocat LOCTYPE="URL" xlin:href="1.tif" xmlns:xlin="http://www.w3.org/1999/xlink"/>
      </mets:file>
    </mets:fileGrp>
    <mets:fileGrp ID="REP3">
      <mets:file ID="FL3">
        <mets:FLocat LOCTYPE="URL" xlin:href="2.jpg" xmlns:xlin="http://www.w3.org/1999/xlink"/>
      </mets:file>
    </mets:fileGrp>
  </mets:fileSec>
</mets:mets>


我想仅将具有jpg扩展名-> FL1,FL3的文件作为输出ID。

我的xsl文件有问题:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:mets="http://www.loc.gov/METS/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mods="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-0.xsd"
    exclude-result-prefixes="xlin">
    <xsl:output method="html" omit-xml-declaration="yes" indent="yes" />

    <xsl:template match="/">
        <xsl:for-each select="//mets:fileSec/mets:fileGrp/mets:file">
            <xsl:variable name="currentID" select="@ID" />
            <xsl:for-each select="//mets:fileSec/mets:fileGrp/mets:file/mets:FLocat">
                <xsl:variable name="testVariable" select="@xlink:href" xmlns:xlink="http://www.w3.org/1999/xlink" />
                <xsl:choose>
                    <xsl:when test="contains($testVariable, '.jpg')"><xsl:value-of select="$currentID"/>,</xsl:when>
                 </xsl:choose>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>


但是经过变换后,我得到如下错误输出:FL1,FL1,FL2,FL2,FL3,FL3,

请帮助我使用XSL。
谢谢!

最佳答案

这样的事情对您有用吗?

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mets="http://www.loc.gov/METS/"
xmlns:xlin="http://www.w3.org/1999/xlink">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:for-each select="mets:mets/mets:fileSec/mets:fileGrp/mets:file[contains(mets:FLocat/@xlin:href, '.jpg')]">
        <xsl:value-of select="@ID"/>
        <xsl:if test="position()!=last()">
            <xsl:text>, </xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

09-03 20:01