簡體   English   中英

使用變量作為屬性值,通過 XSLT 將新元素集添加到現有 XML 文件

[英]Add new element set to existing XML files via XSLT using variables as attribute values

我不太習慣使用 XSLT,但我想在現有的 XML 文件中添加一些新元素,如下所示:

    <TEI>
     <teiHeader>
      ...
      <profileDesc/>
      ...
     </teiHeader>
    ...
     <body/>
    </TEI>

我要添加的是這個(到profileDesc):

        <tei:particDesc>
            <tei:listPerson>
                <tei:person role="" ref="{$persons}">
                    <tei:persName></tei:persName>
                </tei:person>
            </tei:listPerson>
            <tei:listOrg>
                <tei:org role="" ref="{$institutions}">
                    <tei:orgName></tei:orgName>
                </tei:org>
            </tei:listOrg>
        </tei:particDesc>

所以我想添加 particDesc 元素並將 $persons 和 $institutions 的不同值添加到 ref 屬性中。

我的 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"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="xs"
    version="1.0">

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

<xsl:template match="tei:profileDesc">
        <xsl:variable name="persons" select=".//body//rs[@type='person'][not(.=preceding::*)]"/>
        <xsl:variable name="institutions" select=".//body//rs[@type='institution'][not(.=preceding::*)]"/>
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
            <tei:particDesc>
                <tei:listPerson>
                    <tei:person role="" ref="{$persons}">
                        <tei:persName></tei:persName>
                    </tei:person>
                </tei:listPerson>
                <tei:listOrg>
                    <tei:org role="" ref="{$institutions}">
                        <tei:orgName></tei:orgName>
                    </tei:org>
                </tei:listOrg>
            </tei:particDesc>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

當前 output:

<tei:particDesc xmlns:tei="http://www.tei-c.org/ns/1.0"><tei:listPerson><tei:person role="" ref=""><tei:persName/></tei:person></tei:listPerson><tei:listOrg><tei:org role="" ref=""><tei:orgName/></tei:org></tei:listOrg></tei:particDesc></profileDesc>

所需的 output:

<particDesc>
 <listPerson>
  <person role="" ref="[value of distinct persons-value 1]">
   <persName></persName>
  </person>
  <person role="" ref="[value of distinct persons-value 2]">
   <persName></persName>
  </person>
  ...
 </listPerson>
 <listOrg>
  <org role="" ref="[value of distinct institutions-value 1]">
   <orgName></orgName>
  </org>
  <org role="" ref="[value of distinct institutions-value 2]">
   <orgName></orgName>
  </org>
  ...
 </listOrg>
</particDesc>

提前致謝!

您的輸入<profileDesc/>元素為空,因此.//body什么都不選擇。 也許您的意思是select="//body"select="../../body" ,或者正如 Martin 建議的那樣, select="../../tei:body"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM