根据节点在前一节点中出现的顺序,在节点中按顺序编号占位符

问题描述 投票:0回答:1

我有以下XML片段,我想使用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>

这是导出的翻译记忆库的示例,AKA是TMX文件。

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

<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>

但是,这是我所拥有的:

<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代码的主要部分:

<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>
xml xslt xslt-1.0
1个回答
0
投票

我想知道为什么你需要这个,当你已经有type属性链接这两个。无论如何,为什么不这样试试呢:

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>
© www.soinside.com 2019 - 2024. All rights reserved.