2014-09-03 55 views
0

早上好将这些以不同的顺序,我有以下的(双语)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> 

任何援助将不胜感激。

+0

为什么你再次问同样的问题? http://stackoverflow.com/questions/25378898/numbering-placeholders-sequentally-in-a-node-based-on-sequence-in-which-they-ap – 2014-09-03 15:02:01

回答

0

要回答你的最直接的问题,你所得到的数字2,4的原因,6是因为你如何使用xsl:number命令

<xsl:attribute name="x"> 
    <xsl:number/> 
</xsl:attribute> 

你在这一点上放置一个ele元素,所以会计算所有ele元素,当它看起来像你只想计算带有子元素ph的元素时。因此,你需要做到这一点

<xsl:attribute name="x"> 
    <xsl:number count="ele[ph]"/> 
</xsl:attribute> 

然而,回答关于编号第二tuv元素你的下一个问题之前,你需要了解XSLT恒等变换。与其为您希望复制的每个节点编写明确的模板,您只需一个覆盖您希望更改而无需修改的所有模板。所以,你可以写你的当前样式表是眼前这个......

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes" /> 

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

    <xsl:template match="tuv[1]/seg/ele[ph]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <ph type="{ph/@type}"> 
       <xsl:attribute name="x"> 
        <xsl:number count="ele[ph]"/> 
       </xsl:attribute> 
      </ph> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

但要重新数第二tuv元素,可以考虑使用一个密钥来查找ele元素在第一tuv元素

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

然后,做了编号,你将不得不指望前面的兄弟姐妹的数量为每个元素

<xsl:attribute name="x"> 
     <xsl:value-of select="count(key('ph', ../@type)/../preceding-sibling::ele[ph]) + 1" /> 
    </xsl:attribute> 

这将在tuv元素中工作。

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes" /> 

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

    <xsl:strip-space elements="*" /> 

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

    <xsl:template match="tuv/seg/ele/ph/@x"> 
     <xsl:attribute name="x"> 
      <xsl:value-of select="count(key('ph', ../@type)/../preceding-sibling::ele[ph]) + 1" /> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 
+0

伟大的,那工作的一种享受!:)我想进一步尝试一下,并尝试让它在同一个XML文件上工作,然后减去ele元素,我自己添加这些元素使编号更易于执行。你认为这可能实现吗? – user3289842 2014-09-03 08:17:10

+0

是的,那应该是可以的!如果你不能解决问题,请随时提出一个全新的问题! – 2014-09-03 08:33:03

相关问题