更改xlmns值而不使用xslt修改前缀(使用内置模板)

时间:2017-04-13 10:57:59

标签: xslt-2.0 xml-namespaces

源XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<PREMIS:premis xmlns:PREMIS="info:lc/xmlns/premis-v2" version="2.2">
    <PREMIS:object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="PREMIS:file">
        <PREMIS:objectIdentifier>
            <PREMIS:objectIdentifierType>Docuteam</PREMIS:objectIdentifierType>
            <PREMIS:objectIdentifierValue>_20161027171024801</PREMIS:objectIdentifierValue>
        </PREMIS:objectIdentifier>
        <PREMIS:objectCharacteristics>
            <PREMIS:compositionLevel>0</PREMIS:compositionLevel>
            <PREMIS:fixity>
                <PREMIS:messageDigestAlgorithm>SHA-512</PREMIS:messageDigestAlgorithm>
                <PREMIS:messageDigest>2b9be7ebeae4135b0002cfcd7ee4ee2f5d93e80bfabebf6d5d409e504ad1cbd920487f56726362c2a2979b68d96b1c26f37a73e68c30dd9f8cf11502c634ff5a</PREMIS:messageDigest>
            </PREMIS:fixity>
            <PREMIS:size>32783388</PREMIS:size>
            <PREMIS:format>
                <PREMIS:formatDesignation>
                    <PREMIS:formatName>Tagged Image File Format</PREMIS:formatName>
                </PREMIS:formatDesignation>
                <PREMIS:formatRegistry>
                    <PREMIS:formatRegistryName>PRONOM</PREMIS:formatRegistryName>
                    <PREMIS:formatRegistryKey>fmt/353</PREMIS:formatRegistryKey>
                </PREMIS:formatRegistry>
            </PREMIS:format>
        </PREMIS:objectCharacteristics>
        <PREMIS:originalName xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
            >12448399.tif</PREMIS:originalName>
    </PREMIS:object>
    <PREMIS:event>
        <PREMIS:eventIdentifier>
            <PREMIS:eventIdentifierType>Docuteam</PREMIS:eventIdentifierType>
            <PREMIS:eventIdentifierValue>_20161027171025082</PREMIS:eventIdentifierValue>
        </PREMIS:eventIdentifier>
        <PREMIS:eventType>Creation</PREMIS:eventType>
        <PREMIS:eventDateTime>2016-10-27T17:10:25</PREMIS:eventDateTime>
        <PREMIS:eventDetail>Performed by: &apos;INGEST-01$&apos;</PREMIS:eventDetail>
        <PREMIS:eventOutcomeInformation>
            <PREMIS:eventOutcome>Success</PREMIS:eventOutcome>
        </PREMIS:eventOutcomeInformation>
        <PREMIS:linkingObjectIdentifier xmlns:xlink="http://www.w3.org/1999/xlink"
            xlink:type="simple">
            <PREMIS:linkingObjectIdentifierType>Docuteam</PREMIS:linkingObjectIdentifierType>
            <PREMIS:linkingObjectIdentifierValue>_20161027171024801</PREMIS:linkingObjectIdentifierValue>
        </PREMIS:linkingObjectIdentifier>
    </PREMIS:event>
    <PREMIS:event>
        <PREMIS:eventIdentifier>
            <PREMIS:eventIdentifierType>Docuteam</PREMIS:eventIdentifierType>
            <PREMIS:eventIdentifierValue>_20161027171031973</PREMIS:eventIdentifierValue>
        </PREMIS:eventIdentifier>
        <PREMIS:eventType>Fixity Check</PREMIS:eventType>
        <PREMIS:eventDateTime>2016-10-27T17:10:31</PREMIS:eventDateTime>
        <PREMIS:eventDetail>Based on sa_ub-erara-01_dss-01. Performed by:
            &apos;INGEST-01$&apos;</PREMIS:eventDetail>
        <PREMIS:eventOutcomeInformation>
            <PREMIS:eventOutcome>Success</PREMIS:eventOutcome>
        </PREMIS:eventOutcomeInformation>
        <PREMIS:linkingObjectIdentifier xmlns:xlink="http://www.w3.org/1999/xlink"
            xlink:type="simple">
            <PREMIS:linkingObjectIdentifierType>Docuteam</PREMIS:linkingObjectIdentifierType>
            <PREMIS:linkingObjectIdentifierValue>_20161027171024801</PREMIS:linkingObjectIdentifierValue>
        </PREMIS:linkingObjectIdentifier>
    </PREMIS:event>
</PREMIS:premis>

我的XSLT:

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:PREMIS="info:lc/xmlns/premis-v2"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
  >
  <xsl:output method="xml"/>
  <xsl:template match="/">
    <rdf:RDF>
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="'&#10;'"/>
      <xsl:apply-templates/>     
    </rdf:RDF>
  </xsl:template>

  <xsl:template match="PREMIS:premis">
    <xsl:apply-templates select="PREMIS:object"/>
    <xsl:value-of select="'&#10;'"/>
    <xsl:apply-templates select="PREMIS:event"/>
    <xsl:value-of select="'&#10;'"/>
  </xsl:template>   

  <xsl:template match="PREMIS:object">  

    <!-- Verknüpfung zwischen Fedora- und PREMIS-Objekt -->
    <!-- Der Identifikator des Fedoraobjektes "info:fedora/CH-001898-1:X" ist nicht im PREMIS-XML gespeichert, sondern muss irgendwie extern eingefügt werden -->

    <rdf:Description rdf:about="info:fedora/CH-001898-1:7">
      <PREMIS:hasObject><xsl:value-of select="PREMIS:objectIdentifier/PREMIS:objectIdentifierValue"/></PREMIS:hasObject>
    </rdf:Description>

    <xsl:value-of select="'&#10;'"/>

    <!-- PREMIS:object -->
    <!-- Ebene1 -->

    <rdf:Description>
      <xsl:attribute name="rdf:about">
        <xsl:value-of select="PREMIS:objectIdentifier/PREMIS:objectIdentifierValue"/>
      </xsl:attribute>
      <PREMIS:hasIdentifier><xsl:value-of select="PREMIS:objectIdentifier/PREMIS:objectIdentifierValue"/><xsl:text>objectIdentifier</xsl:text></PREMIS:hasIdentifier>
      <PREMIS:hasOriginalName><xsl:value-of select="PREMIS:originalName"/></PREMIS:hasOriginalName>
    </rdf:Description>

    <xsl:value-of select="'&#10;'"/>

    <!-- Ebene2 -->

    <rdf:Description>
      <xsl:attribute name="rdf:about">
        <xsl:value-of select="PREMIS:objectIdentifier/PREMIS:objectIdentifierValue"/><xsl:text>objectIdentifier</xsl:text></xsl:attribute>
      <PREMIS:hasIdentifierType><xsl:value-of select="PREMIS:objectIdentifier/PREMIS:objectIdentifierType"/></PREMIS:hasIdentifierType>
      <PREMIS:hasIdentifierValue><xsl:value-of select="PREMIS:objectIdentifier/PREMIS:objectIdentifierValue"/></PREMIS:hasIdentifierValue>
    </rdf:Description>

    <xsl:value-of select="'&#10;'"/>
  </xsl:template>
  <!-- Events -->
  <!-- Ebene1 -->
  <xsl:template match="PREMIS:event">     
    <rdf:Description>
      <xsl:attribute name="rdf:about">
        <xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/></xsl:attribute>
      <PREMIS:hasIdentifier><xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/></PREMIS:hasIdentifier>
      <PREMIS:EventDateTime><xsl:value-of select="PREMIS:eventDateTime"/></PREMIS:EventDateTime>
      <PREMIS:EventDetail><xsl:value-of select="PREMIS:eventDetail"/></PREMIS:EventDetail>
      <PREMIS:hasEventOutcomeInformation><xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/><xsl:text>eventOutcomeInformation</xsl:text></PREMIS:hasEventOutcomeInformation>
      <PREMIS:hasEventLinkingObjectIdentifier><xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/><xsl:text>eventLinkingObjectIdentifier</xsl:text></PREMIS:hasEventLinkingObjectIdentifier>
    </rdf:Description>

    <xsl:value-of select="'&#10;'"/>

    <!-- Ebene2 --> 

    <rdf:Description>
      <xsl:attribute name="rdf:about">
        <xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/><xsl:text>eventIdentifier</xsl:text></xsl:attribute>
      <PREMIS:hasIdentifierType><xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierType"/></PREMIS:hasIdentifierType>
      <PREMIS:hasIdentifierValue><xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/></PREMIS:hasIdentifierValue>
    </rdf:Description>

    <xsl:value-of select="'&#10;'"/>

    <rdf:Description>
      <xsl:attribute name="rdf:about">
        <xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/><xsl:text>eventOutcomeInformation</xsl:text></xsl:attribute>
      <PREMIS:hasEventOutcome><xsl:value-of select="PREMIS:eventOutcomeInformation/PREMIS:eventOutcome"/></PREMIS:hasEventOutcome>
    </rdf:Description>

    <xsl:value-of select="'&#10;'"/>  

    <rdf:Description>
      <xsl:attribute name="rdf:about">
        <xsl:value-of select="PREMIS:eventIdentifier/PREMIS:eventIdentifierValue"/><xsl:text>eventLinkingObjectIdentifier</xsl:text></xsl:attribute>
      <PREMIS:linkingObjectIdentifierType><xsl:value-of select="PREMIS:linkingObjectIdentifier/PREMIS:linkingObjectIdentifierType"/></PREMIS:linkingObjectIdentifierType>
      <PREMIS:hasLinkingObjectIdentifierValue><xsl:value-of select="PREMIS:linkingObjectIdentifier/PREMIS:linkingObjectIdentifierValue"/></PREMIS:hasLinkingObjectIdentifierValue>
    </rdf:Description>
    <xsl:value-of select="'&#10;'"/>

  </xsl:template>

</xsl:stylesheet>

在输出xml文档中,我希望将xmlns:PREMIS的值更改为&#34; http://www.loc.gov/premis/rdf/v1#&#34;。因此,开放根元素应如下所示:

<rdf:RDF xmlns:PREMIS="info:lc/xmlns/premis-v2" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:xlink="http://www.w3.org/1999/xlink">

我已经尝试了几件事,也是我在这里发现的帖子。但我不明白。有任何想法吗?感谢。

后处理XSLT:

<?xml version="1.0"?>
<!-- sample_2.xsl -->
<xsl:stylesheet version="2.0"
exclude-result-prefixes="PREMIS"
xmlns:PREMIS="info:lc/xmlns/premis-v2"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
>

<xsl:template match="*">
    <xsl:copy>
        <xsl:copy-of select="namespace::* except namespace::PREMIS"/>
        <xsl:namespace name="PREMIS" select="'http://www.loc.gov/premis/rdf/v1#'"/>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="PREMIS:*">
    <xsl:element name="PREMIS:{local-name()}" namespace="http://www.loc.gov/premis/rdf/v1#">
        <xsl:copy-of select="namespace::* except namespace::PREMIS"/>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template></xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我假设你想要的输出是一个错字,你真的想要这个:

<rdf:RDF xmlns:PREMIS="http://www.loc.gov/premis/rdf/v1#" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:xlink="http://www.w3.org/1999/xlink">
  ...
</rdf:RDF>

在样式表中,使用相同的前缀PREMIS来引用不同的名称空间,具体取决于您是在阅读还是写作,这将非常困难。输出中的前缀是否真的必须是PREMIS,还是其他一些前缀呢?如果它确实必须相同(也许无论如何),我认为我的首选方法是使用以下方式对输出进行后处理:

<xsl:stylesheet exclude-result-prefixes="PREMIS"...>

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="namespace::* except namespace::PREMIS"/>
    <xsl:namespace name="PREMIS" select="'http://www.loc.gov/premis/rdf/v1#'"/>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="PREMIS:*">
  <xsl:element name="PREMIS:{local-name()}" namespace="http://www.loc.gov/premis/rdf/v1#">
    <xsl:copy-of select="namespace::* except namespace::PREMIS"/>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>