2

首先让我说我不知道​​我在用 XSLT 做什么。我从其他人那里继承的所有 XSLT。我有一些 XML(如果有帮助,那就是 EAD),它不是我们的样式表所期望的那样形成的,因此它不会正确地转换为 XHTML。

基本上,我需要<unitdate>成为 的孩子,而<unittitle>不是它的兄弟姐妹。

大部分文档如下所示:

<c03 id="ref13" level="file">
<did>
<unittitle>1. President (White House)</unittitle>
<container id="cid192710" type="Box" label="Text">1</container>
<container parent="cid192710" type="Folder">2</container>
<unitdate normal="1953/1956" type="inclusive">1953-1956</unitdate>
</did>
</c03>

我需要它看起来像这样:

<c03 id="ref13" level="file">
<did>
<unittitle>1. President (White House)<unitdate normal="1953/1956" type="inclusive">1953-1956</unitdate></unittitle>
<container id="cid192710" type="Box" label="Text">1</container>
<container parent="cid192710" type="Folder">2</container>
</did>
</c03>

有没有一种简单的方法可以做到这一点?我知道有类似的问题,但我对这一点的理解还不够好,无法对其进行调整以使其正常工作。谢谢。

4

2 回答 2

2

试试这些模板:

<xsl:template match="unittitle[following-sibling::unitdate]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    <xsl:copy-of select="following-sibling::unitdate"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="unitdate"/>

以及身份模板:

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

身份模板完全按原样复制所有内容。在特定情况下,前面的两个模板会覆盖它,其中您有一个unittitle元素unitdate后面有一个兄弟元素,以及unitdate元素本身。

您可能会注意到第一个模板几乎与身份模板相同——这是因为它复制身份的方式与身份模板相同,除了在处理完其他所有内容(即文本)之后unittitle,它还复制以下元素。unitdate

单行unitdate模板只是通过处理将其从原来的位置移除,而不输出任何内容。

于 2013-11-13T16:32:59.677 回答
0

要不修改现有的 XSLT 并且仍然能够处理正在处理的 XML,您可以使用以下 XSLT (1.0) 段来解决您的问题。

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="*">
      <xsl:copy>
    <xsl:apply-templates />
      </xsl:copy>
  </xsl:template>

  <xsl:template match="did">
      <xsl:copy>
         <xsl:apply-templates select="*[local-name()!='unitdate']"/>
      </xsl:copy>
  </xsl:template>

   <xsl:template match="unittitle">
       <xsl:element name="unittitle">
             <xsl:value-of select="text()"/>
             <xsl:copy-of select="../unitdate" />
       </xsl:element>
  </xsl:template>

  <xsl:template match="@*|text()|comment()|processing-instruction">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

但是您应该认真考虑不要使用它。修改现有的 XSLT 是合理的做法。这只会产生性能开销。

于 2013-11-13T16:29:07.107 回答