根据占位符在前一个节点中出现的顺序,在节点中按顺序编号占位符 [英] Numbering placeholders sequentially in a node, based on sequence in which they appear in a preceding node

查看:16
本文介绍了根据占位符在前一个节点中出现的顺序,在节点中按顺序编号占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 片段,我想使用 XSLT 对其进行转换:

I have the following snippet of XML, which I would like to transform using XSLT:

<?xml version="1.0" encoding="utf-8"?>
<tmx version="1.4">
<body>
<tu>
  <prop type="x-Context">-2050338055591740051, -2050338055591740051</prop>
  <prop type="x-Origin">TM</prop>
  <prop type="x-ConfirmationLevel">Translated</prop>
  <tuv xml:lang="en-US">
    <seg>The text <ph x="0" type="QIAsymphony" /> goes <ph x="0" type="470" /> here <ph x="0" type="471" />.</seg>
  </tuv>
  <tuv xml:lang="es-ES">
    <seg>El texto <ph x="0" type="QIAsymphony" /> se mete <ph x="0" type="471" /> aquí <ph x="0" type="470" />.</seg>
  </tuv>
</tu>
</body>
</tmx>

这是一个导出的翻译记忆库示例,也就是 TMX 文件.

This is a sample of an exported translation memory, AKA a TMX file.

我需要首先对第一个 tuv 节点中元素 ph 的 x 属性进行编号,这是我设法做到的.然后我需要将这些序号应用到第二个tuv节点中的ph元素上,其中x属性值对应的是type属性值(请注意第二个tuv节点中的元素顺序不同):

I need to first of all number sequentially the x attributes of element ph in the first tuv node, which I've managed to do. Then I need to apply these sequence numbers to the ph elements in the second tuv node, where the x attribute value corresponds to the type attribute value (please note that the elements in the second tuv node are in a different sequence):

<tuv xml:lang="en-US">
    <seg>The text <ph x="0" type="QIAsymphony" /> goes <ph x="0" type="470" /> here <ph x="0" type="471" />.</seg>
  </tuv>

即我想要实现的是:

<tuv><seg>The text <ph x="1" type="QIAsymphony"/> goes <ph x="2" type="470"/> here <ph x="3" type="471"/>.</seg></tuv><tuv><seg>El texto <ph x="1" type="QIAsymphony"/> se mete <ph x="3" type="471"/> aquí <ph x="2" type="470"/>.</seg></tuv>

但是,这是我所拥有的:

However, this is as far as I've got:

<tuv><seg>The text <ph x="1" type="QIAsymphony"/> goes <ph x="2" type="470"/> here <ph x="3" type="471"/>.</seg></tuv><tuv><seg>El texto <ph x="" type="QIAsymphony"/> se mete <ph x="" type="471"/> aquí <ph x="" type="470"/>.</seg></tuv>

这是我的 XSLT 代码的主要部分:

Here's the main part of my XSLT code:

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

</xsl:template>
<xsl:template match="ph"> 


<xsl:choose>
    <xsl:when test="ancestor::tuv/following-sibling::tuv">
            <ph><xsl:attribute name="x">
            <xsl:number/>

            </xsl:attribute><xsl:attribute name="type"><xsl:value-of select="@type"/></xsl:attribute></ph>
                <xsl:apply-templates/>
    </xsl:when>
</xsl:choose>
<xsl:choose>
    <xsl:when test="ancestor::tuv/preceding-sibling::tuv">
        <ph><xsl:attribute name="x">
<xsl:choose>


<xsl:when test="./@type = ancestor::tuv/preceding-sibling::tuv/seg/ph/@type">


<xsl:choose>
<xsl:when test=".">
<xsl:copy-of select="./@x"/>


</xsl:when>
</xsl:choose>


</xsl:when>
</xsl:choose>

</xsl:attribute><xsl:attribute name="type"><xsl:value-of select="@type"/></xsl:attribute></ph>


 <xsl:apply-templates/>
 </xsl:when>

</xsl:choose>

</xsl:template>

推荐答案

我想知道为什么你需要这个,当你已经有 type 属性将两者联系起来时.无论如何,为什么不这样尝试:

I wonder why you need this, when you already have the type attribute linking the two. Anyway, why not try it this way:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="k1" match="tuv[1]/seg/ph" use="@type" />

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

<xsl:template match="tuv[1]/seg/ph">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="x"><xsl:value-of select="count(preceding-sibling::ph) + 1"/></xsl:attribute>
    </xsl:copy>
</xsl:template>

<xsl:template match="tuv[position() > 1]/seg/ph">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="x"><xsl:value-of select="count(key('k1', @type)/preceding-sibling::ph) + 1"/></xsl:attribute>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这篇关于根据占位符在前一个节点中出现的顺序,在节点中按顺序编号占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆