0

我正在对一些 EAD(编码存档描述)XML 文档执行 XSLT 2.0 身份转换。我需要稍微修改输出,但我似乎遇到了命名空间问题。

不要在模式感知处理器中测试它,你会得到错误!:)

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="2.0">

<xsl:output method="xml" indent="yes" standalone="no"/>

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

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

Sample-EAD.xml(也可在此处的 pastebin 上找到:http: //pastebin.com/RFAQaY3w

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ead xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd"     xmlns:ns2="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-22-9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511" countryencoding="iso3166-1" dateencoding="iso8601" langencoding="iso639-2b">  
<eadid>01234</eadid>
<filedesc>
    <AAA>1</AAA>
    <BBB>2</BBB>
</filedesc>
</eadheader>
<archdesc>
    <bib>
        <CCC>1</CCC>
        <DDD>2</DDD>
    </bib>
</archdesc>
 </ead>

如果我从元素中提取名称空间信息(也可在此处获得:http: //pastebin.com/6ygi3xUm),则带有修改的身份转换将起作用。

样品-EAD-2:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ead>

<eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511" countryencoding="iso3166-1" dateencoding="iso8601" langencoding="iso639-2b">  
<eadid>01234</eadid>
<filedesc>
    <AAA>1</AAA>
    <BBB>2</BBB>
</filedesc>
</eadheader>
<archdesc>
    <bib>
        <CCC>1</CCC>
        <DDD>2</DDD>
    </bib>
</archdesc>

我对命名空间不是很好,所以任何建议都将不胜感激。如果我尝试将命名空间添加到 XSLT,则转换可以工作,但我会在新元素中获得命名空间属性。非常感谢您的阅读和您的建议!干杯!

编辑:输入(w/namespaces),通过身份转换结果运行——

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ead xmlns:ns2="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-22-9"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd">

<eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511"
          countryencoding="iso3166-1"
          dateencoding="iso8601"
          langencoding="iso639-2b"> 
     <eadid>01234</eadid>
     <filedesc>
           <AAA>1</AAA>
           <BBB>2</BBB>
     </filedesc>
  </eadheader>
  <archdesc>
        <bib>
              <CCC>1</CCC>
              <DDD>2</DDD>
        </bib>
  </archdesc>
</ead>

输入(无命名空间)贯穿身份转换结果——

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ead>

<eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511"
          countryencoding="iso3166-1"
          dateencoding="iso8601"
          langencoding="iso639-2b"> 
     <eadid>01234</eadid>
     <filedesc>
     <header>Subjects</header>
           <AAA>1</AAA>
           <BBB>2</BBB>
     </filedesc>
  </eadheader>
  <archdesc>
        <bib>
              <CCC>1</CCC>
              <DDD>2</DDD>
        </bib>
  </archdesc>

最后,输入(带命名空间)通过 XSLT(添加命名空间)结果运行——

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="2.0"
xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd"
xmlns:ns2="http://www.w3.org/1999/xlink" 
xmlns="urn:isbn:1-931666-22-9"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:output method="xml" indent="yes" standalone="no"/>

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

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

</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ead xmlns:ns2="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-22-9"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd">

<eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511"
          countryencoding="iso3166-1"
          dateencoding="iso8601"
          langencoding="iso639-2b"> 
     <eadid>01234</eadid>
     <filedesc>
           <AAA>1</AAA>
           <BBB>2</BBB>
     </filedesc>
  </eadheader>
  <archdesc>
        <bib>
              <CCC>1</CCC>
              <DDD>2</DDD>
        </bib>
  </archdesc>
</ead>

编辑#2:抱歉,所有,因为我认为我未能表达我想要的输出。我想避免在修改后的元素中添加额外的属性,因为在身份转换之后会进行额外的处理,并且我想让 XML 尽可能接近它的当前状态(编辑除外)。再次感谢您的耐心和帮助!这是一个例子:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ead xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd" xmlns:ns2="http://www.w3.org/1999/xlink" xmlns="urn:isbn:1-931666-22-9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<eadheader findaidstatus="Ready_for_online" repositoryencoding="iso15511" countryencoding="iso3166-1" dateencoding="iso8601" langencoding="iso639-2b">  
    <eadid>01234</eadid>
    <filedesc>
        <header>List Name</header>
        <AAA>1</AAA>
        <BBB>2</BBB>
    </filedesc>
</eadheader>
<archdesc>
    <bib>
        <CCC>1</CCC>
        <DDD>2</DDD>
    </bib>
</archdesc>
</ead>
4

1 回答 1

0

这是一个很长的问题,有点难以理解,但我认为这就是你想要的。问题是您要匹配的元素位于源 xml 文档的命名空间中,因此您也必须匹配命名空间。因此,我在 xslt: 中为其分配了一个前缀:xmlns:input="urn:isbn:1-931666-22-9"然后我使用它来匹配 filedesc 元素:<xsl:template match="input:filedesc">。我还将相同的命名空间设置为 xsl 的默认命名空间,因此您的元素将位于该命名空间中。

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:input="urn:isbn:1-931666-22-9"
    xmlns="urn:isbn:1-931666-22-9"
    exclude-result-prefixes="input"
    version="2.0">

  <xsl:output method="xml" indent="yes" standalone="no"/>

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

  <xsl:template match="input:filedesc">
    <xsl:copy>
      <header>Subjects</header>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>  
</xsl:stylesheet>  
于 2012-04-17T21:23:51.277 回答