早上好將這些以不同的順序,我有以下的(雙語)XML片段:運用順序編號作爲子節點的屬性值在一個父節點,然後在兄弟節點
<?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>
<seg>
<ele>The text </ele>
<ele>
<ph x="0" type="QIAsymphony"/>
</ele>
<ele> goes </ele>
<ele>
<ph x="0" type="470"/>
</ele>
<ele> here </ele>
<ele>
<ph x="0" type="471"/>
</ele>
<ele>.</ele>
</seg>
</tuv>
<tuv>
<seg>
<ele>El texto </ele>
<ele>
<ph x="0" type="QIAsymphony"/>
</ele>
<ele> se mete </ele>
<ele>
<ph x="0" type="471"/>
</ele>
<ele> aquí </ele>
<ele>
<ph x="0" type="470"/>
</ele>
<ele>.</ele>
</seg>
</tuv>
</tu>
</body>
</tmx>
我首先想要將第一個tuv/seg節點中ph元素的x屬性值從1到3(在這種情況下)編號。
但是,我得到的結果是這樣的:
<tuv>
<seg>
<ele>The text </ele>
<ele>
<ph x="2" type="QIAsymphony"/>
</ele>
<ele> goes </ele>
<ele>
<ph x="4" type="470"/>
</ele>
<ele> here </ele>
<ele>
<ph x="6" type="471"/>
</ele>
<ele>.</ele>
</seg>
</tuv>
這是基於以下XSLT樣式表:
<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:template match="tmx">
<tmx><xsl:attribute name="version"><xsl:value-of select="./@version"/></xsl:attribute>
<xsl:apply-templates/>
</tmx>
</xsl:template>
<xsl:template match="body">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="tuv">
<tuv>
<xsl:apply-templates/>
</tuv>
</xsl:template>
<xsl:template match="prop">
<prop><xsl:attribute name="type"><xsl:value-of select="./@type"/></xsl:attribute>
<xsl:apply-templates/>
</prop>
</xsl:template>
<xsl:template match="tu">
<tu>
<xsl:apply-templates/>
</tu>
</xsl:template>
<xsl:template match="tuv[1]/seg">
<seg>
<xsl:for-each select="ele">
<xsl:choose>
<xsl:when test="child::ph">
<ele><ph><xsl:attribute name="x">
<xsl:number/>
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select="ph/@type"/>
</xsl:attribute>
</ph></ele>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="child::text()">
<xsl:copy-of select="."/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</seg>
</xsl:template>
</xsl:stylesheet>
爲了話,我需要以下結果:
<tuv>
<seg>
<ele>The text </ele>
<ele>
<ph x="1" type="QIAsymphony"/>
</ele>
<ele> goes </ele>
<ele>
<ph x="2" type="470"/>
</ele>
<ele> here </ele>
<ele>
<ph x="3" type="471"/>
</ele>
<ele>.</ele>
</seg>
</tuv>
最後,根據第一個tuv/seg節點中的類型屬性值,我需要應用相應的荷蘭國際集團的x值與x在第二TUV/SEG節點(在這種情況下將是不同的順序)屬性:
<tuv>
<seg>
<ele>El texto </ele>
<ele>
<ph x="1" type="QIAsymphony"/>
</ele>
<ele> se mete </ele>
<ele>
<ph x="3" type="471"/>
</ele>
<ele> aquí </ele>
<ele>
<ph x="2" type="470"/>
</ele>
<ele>.</ele>
</seg>
</tuv>
任何援助將不勝感激。
爲什麼你再次問同樣的問題? http://stackoverflow.com/questions/25378898/numbering-placeholders-sequentally-in-a-node-based-on-sequence-in-which-they-ap – 2014-09-03 15:02:01