如何用XSLT替换XML中的部分行?

时间:2018-01-16 17:30:37

标签: xml xslt replace substitution

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<mets:mets xmlns:mets="http://www.loc.gov/METS/">
 <mets:fileSec>
    <mets:fileGrp ID="rep1" ADMID="rep1-amd">
      <mets:file ID="fid1-1" ADMID="fid1-1-amd">
        <mets:FLocat xmlns:xlin="http://www.w3.org/1999/xlink" LOCTYPE="URL" xlin:href="/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV631313192/content/streams/MASTER/Prandtl_4.pdf"/>

      </mets:file>
    </mets:fileGrp>
    <mets:fileGrp ID="rep2" ADMID="rep2-amd">
      <mets:file ID="fid1-2" ADMID="fid1-2-amd">
        <mets:FLocat xmlns:xlin="http://www.w3.org/1999/xlink" LOCTYPE="URL" xlin:href="/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV631313192/content/streams/DERIVATIVE_COPY/631313192.pdf"/>
      </mets:file>
    </mets:fileGrp>
  </mets:fileSec>
</mets:mets> 

对于每一个mets:FLocat我想做以下改变:

现在:  <mets:FLocat xmlns:xlin="http://www.w3.org/1999/xlink" LOCTYPE="URL" xlin:href="/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV631313192/content/streams/MASTER/Prandtl_4.pdf"/>

必须是:<mets:FLocat LOCTYPE="URL" xlin:href="file://MASTER/Prandtl_4.pdf" xmlns:xlin="http://www.w3.org/1999/xlink"/>

因此,我们删除了/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV631313192/content/streams,而不是添加file://

这是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

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


 <xsl:for-each select="/mets:mets/mets:filesec/mets:fileGrp/mets:file/mets:Flocat"> 
    <xsl:value-of select="xlin:href"/>
</xsl:for-each> 
    </xsl:template>

</xsl:stylesheet>

我对XSLT很新,我不知道如何参考我必须更改的行。也许有些想法?

1 个答案:

答案 0 :(得分:3)

考虑使用XSLT 3(由Saxon 9.8或Altova XMLSpy 2017或2018支持),然后就像

一样简单
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mets="http://www.loc.gov/METS/"
    xmlns:xlin="http://www.w3.org/1999/xlink"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="mets:FLocat/@xlin:href">
      <xsl:attribute name="{name()}" select="'file://' || replace(., '^/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV631313192/content/streams', '')"/>
  </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/nbUY4kn

对于XSLT 2,您需要将身份转换拼写为

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mets="http://www.loc.gov/METS/"
    xmlns:xlin="http://www.w3.org/1999/xlink"
    exclude-result-prefixes="#all"
    version="2.0">

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

  <xsl:template match="mets:FLocat/@xlin:href">
      <xsl:attribute name="{name()}" select="concat('file://', replace(., '^/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV631313192/content/streams', ''))"/>
  </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/nbUY4kn/2

对于XSLT 1,没有一般的字符串替换,但是你想在某个字符串之后使用属性值的末尾,你可以使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mets="http://www.loc.gov/METS/"
    xmlns:xlin="http://www.w3.org/1999/xlink"
    exclude-result-prefixes="xs mets xlin"
    version="1.0">

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

  <xsl:template match="mets:FLocat/@xlin:href">
      <xsl:attribute name="{name()}">
          <xsl:value-of select="concat('file://', substring-after(., '/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV631313192/content/streams'))"/>
      </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/nbUY4kn/3