我试图取代这个XML更换两个过同级元素:XSL:一个
<name type="personal" authority="local">
<namePart>Gertrude</namePart>
<namePart type="termsOfAddress">Aunt</namePart>
<role>
<roleTerm authority="marcrelator" type="text">Correspondent</roleTerm>
<roleTerm authority="marcrelator" type="code">crp</roleTerm>
</role>
</name>
与该XML:
<name type="personal" authority="local">
<namePart>Aunt Gertrude</namePart>
<role>
<roleTerm authority="marcrelator" type="text">Correspondent</roleTerm>
<roleTerm authority="marcrelator" type="code">crp</roleTerm>
</role>
</name>
,同时仍保留了文档的其余部分。我尝试了两种方法:一种可行,但看起来很愚蠢,一种不起作用,而且看起来很愚蠢。
第一种方法
<xsl:template match="* | processing-instruction() | comment()">
<xsl:copy>
<xsl:copy-of select="@*" copy-namespaces="no"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="//name/namePart[matches(., 'Gertrude')
and following-sibling::namePart[@type='termsOfAddress'
and matches(., 'Aunt')]]">
<namePart>Aunt Gertrude</namePart>
</xsl:template>
<xsl:template match="//name/namePart[@type='termsOfAddress'
and matches(., 'Aunt')
and preceding-sibling::namePart[matches(., 'Gertrude')]]"/>
Seoncd方法
<xsl:template match="* | processing-instruction() | comment()">
<xsl:copy>
<xsl:copy-of select="@*" copy-namespaces="no"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="//name[descendant::namePart[matches(., 'Gertrude')]
and descendant::namePart[@type='termsOfAddress'
and matches(., 'Aunt')]]/namePart">
<namePart>Aunt Gertrude</namePart>
</xsl:template>
所以,就像我说的,第一个作品,但有两个单独的模板来处理两个要素显得有些多余。所以我尝试了第二种方法给我这个:
<name type="personal" authority="local">
<namePart>Aunt Gertrude</namePart>
<namePart>Aunt Gertrude</namePart>
<role>
<roleTerm authority="marcrelator" type="text">Correspondent</roleTerm>
<roleTerm authority="marcrelator" type="code">crp</roleTerm>
</role>
</name>
这不是我想要的。
有什么办法可以选择两个namePart元素并将其替换为一个?
有许多方法,但是您不会按什么顺序(排序规则)解释应将值连接成单个字符串的方法。 –