如何在XSLT中正确编辑节点值

问题描述 投票:0回答:1

在下面的XML代码中,我尝试编辑文本节点<key id="fileOriginalPath">/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV626375266/content/streams/DERIVATIVE_COPY/626375266.pdf</key>并将其缩短为:

`<key id="fileOriginalPath">/GBV626375266/content/streams/DERIVATIVE_COPY/626375266.pdf</key>`

所以应删除部分/exlibris1/transfer/lza-tib/submission.tib/ingest这是我的XSLT,但问题是它不会对XML进行任何更改:

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

  <xsl:template match="record/key[@id='fileOriginalPath']/text()[. = '/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV626375266/content/streams/DERIVATIVE_COPY/626375266.pdf']">GBV626375266/content/streams/DERIVATIVE_COPY/626375266.pdf</xsl:template>

</xsl:stylesheet>

那是原始的XML:

<?xml version="1.0" encoding="UTF-8"?><mets:mets xmlns:mets="http://www.loc.gov/METS/">
<mets:amdSec ID="fid1-2-amd">
    <mets:techMD ID="fid1-2-amd-tech">
      <mets:mdWrap MDTYPE="OTHER" OTHERMDTYPE="dnx">
        <mets:xmlData>
          <dnx xmlns="http://www.exlibrisgroup.com/dps/dnx">
            <section id="generalFileCharacteristics">
              <record>
                <key id="label">Das physikalische Praktikum : Handbuch 2010 für Studentinnen und Studenten der Physik ; mit 21 Tabellen</key>
                <key id="fileMIMEType">application/pdf</key>
                <key id="note">Das physikalische Praktikum : Handbuch 2010 für Studentinnen und Studenten der Physik ; mit 21 Tabellen : application/pdf</key>
                <key id="fileOriginalPath">/exlibris1/transfer/lza-tib/submission.tib/ingest/GBV626375266/content/streams/DERIVATIVE_COPY/626375266.pdf</key>
              </record>
            </section>
            <section id="fileFixity">
              <record>
                <key id="fixityType">MD5</key>
                <key id="fixityValue">763faa0ad3bcbdf6618acecbc7044fb3</key>
              </record>
            </section>
          </dnx>
        </mets:xmlData>
      </mets:mdWrap>
    </mets:techMD>
    <mets:rightsMD ID="fid1-2-amd-rights">
      <mets:mdWrap MDTYPE="OTHER" OTHERMDTYPE="dnx">
        <mets:xmlData>
          <dnx xmlns="http://www.exlibrisgroup.com/dps/dnx"/>
        </mets:xmlData>
      </mets:mdWrap>
    </mets:rightsMD>
    <mets:sourceMD ID="fid1-2-amd-source">
      <mets:mdWrap MDTYPE="OTHER" OTHERMDTYPE="dnx">
        <mets:xmlData>
          <dnx xmlns="http://www.exlibrisgroup.com/dps/dnx"/>
        </mets:xmlData>
      </mets:mdWrap>
    </mets:sourceMD>
    <mets:digiprovMD ID="fid1-2-amd-digiprov">
      <mets:mdWrap MDTYPE="OTHER" OTHERMDTYPE="dnx">
        <mets:xmlData>
          <dnx xmlns="http://www.exlibrisgroup.com/dps/dnx"/>
        </mets:xmlData>
      </mets:mdWrap>
    </mets:digiprovMD>
  </mets:amdSec>
 </mets:mets>
xml xslt
1个回答
2
投票

问题是dnx及其下的所有内容都在默认命名空间中。

您需要在XSLT中考虑到这一点,否则您的xpath将在无命名空间中查找元素。

正如您使用XSLT 3.0,您可以使用xpath-default-namespace来处理它。 (这也适用于XSLT 2.0)。

<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"
    xpath-default-namespace="http://www.exlibrisgroup.com/dps/dnx"
    exclude-result-prefixes="#all"
    version="3.0">

顺便说一下,也许你让你的模板更通用......

<xsl:template match="record/key[@id='fileOriginalPath']/text()[starts-with(., '/exlibris1/transfer/lza-tib/submission.tib/ingest')]">
  <xsl:value-of select="substring(., 51)" />
</xsl:template>

编辑:如果你想使用变量(或参数),你可以......

<xsl:param name="path" select="'/exlibris1/transfer/lza-tib/submission.tib/ingest'" />

<xsl:template match="record/key[@id='fileOriginalPath']/text()[starts-with(., $path)]">
  <xsl:value-of select="substring(., string-length($path) + 1" />
</xsl:template>
© www.soinside.com 2019 - 2024. All rights reserved.