XSL转换 - 按字符串内容选择节点ID

时间:2014-02-27 18:15:53

标签: java .net xml xslt transform

我有像这样的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>

我想获得输出ID只有jpg扩展的文件 - &gt; FL1,FL3。

我的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。 谢谢!

1 个答案:

答案 0 :(得分:2)

这样的事情对你有用吗?

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