Numbering placeholders sequentially in a node, bas

2019-09-02 21:13发布

问题:

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>

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

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>

i.e. what I'm trying to achieve is this:

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

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>

回答1:

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>