XSLT копирует дочерний узел, если родительский узел содержит X и Y в качестве дочерних узлов

У меня есть исходный html, например:

<td id="All_Label_Subcategories">
  <source>
    All Label Subcategories
  </source>
  <target>
    target empty
 </target>              
</td>
<td id='node_without_child_nodes'>
   some text
</td>

Ожидаемый целевой xml:

 <trans-unit id="td-1">
   <source>All Label Subcategories</source>
   <target>target empty</target>
 </trans-unit>
 <trans-unit id="td-2">
   <source>some text</source>
   <target>some text</target>
 </trans-unit>

Я использую xslt из xliffRoundTrip Tool, я не уверен, что должен вставлять сюда весь xlst, хотя он с открытым исходным кодом . Часть xslt, которая отвечает за это преобразование и нуждается в изменении:

 <xsl:choose>
     <xsl:when test="not(text())">
      <group xmlns="urn:oasis:names:tc:xliff:document:1.2" id="{concat(generate-id(),'axmark',local-name(),'-',(count(preceding::*)+count(ancestor::*)))}">
       <xsl:apply-templates />
      </group>
     </xsl:when>
     <xsl:otherwise>
      <group xmlns="urn:oasis:names:tc:xliff:document:1.2" id="{concat(generate-id(),'axmark',local-name(),'-',(count(preceding::*)+count(ancestor::*)))}">
       <trans-unit id="{concat(local-name(),'-',(count(preceding::*)+count(ancestor::*)))}">
        <source>
         <xsl:apply-templates />
        </source>
        <target>
         <xsl:apply-templates />
        </target>
       </trans-unit>
      </group>
     </xsl:otherwise>
    </xsl:choose>

Мое намерение состоит в том, чтобы передать пользовательские данные для целевого языка, так как во многих случаях нам нужно пересмотреть целевой язык и передать целевую строку для проверки.

Редактирует Я изменил xslt, но он копируется дважды e. грамм

 <xsl:choose>
     <xsl:when test="not(text()) and not(*[source and target]) ">
         <xsl:choose>
             <xsl:when test="*[source and target]">
               <trans-unit id="{concat(local-name(),'-',(count(preceding::*)+count(ancestor::*)))}">
                 <source match="source">
                   <xsl:apply-templates />
                 </source>
                 <target match="target">
                   <xsl:apply-templates />
                 </target>       
               </trans-unit>
              </xsl:when>
              <xsl:otherwise>
                  <group xmlns="urn:oasis:names:tc:xliff:document:1.2" id="{concat(generate-id(),'axmark',local-name(),'-',(count(preceding::*)+count(ancestor::*)))}">
                   <xsl:apply-templates />
                  </group>
              </xsl:otherwise>    
         </xsl:choose>   
     </xsl:when>     
     <xsl:otherwise>
      <group xmlns="urn:oasis:names:tc:xliff:document:1.2" id="{concat(generate-id(),'axmark',local-name(),'-',(count(preceding::*)+count(ancestor::*)))}">
       <trans-unit id="{concat(local-name(),'-',(count(preceding::*)+count(ancestor::*)))}">
        <source>
         <xsl:apply-templates />
        </source>
        <target>
         <xsl:apply-templates />
        </target>
       </trans-unit>
      </group>
     </xsl:otherwise>
    </xsl:choose>

person sakhunzai    schedule 28.05.2014    source источник


Ответы (2)


<xsl:apply-templates select="//*[source and target]"/>
…
<xsl:template match="td[source and target]">
  <trans-unit id="td-{position()}">
    <xsl:copy-of select="*"/>
  </trans-unit>
<xsl:template>

Может работать. Сначала попробуйте автономный режим, без навороченного инструмента с открытым исходным кодом.

person Lumi    schedule 28.05.2014
comment
он копирует дважды, один раз для источника и один раз для цели. Как избежать перехода через источник и цель, см. Мои правки. Спасибо - person sakhunzai; 28.05.2014

Учитывая исходный файл, например

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <td id="All_Label_Subcategories">
        <source>All Label Subcategories</source>
        <target>target empty</target>
    </td>
    <td id="node_without_child_nodes">some text</td>
</root>

и эта таблица стилей:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/root">
        <xsl:for-each select="td">
            <xsl:choose>
                <xsl:when test="not(text())">
                    <trans-unit id="{concat(local-name(),'-',(count(preceding-sibling::td) + 1))}">
                        <xsl:copy-of select="*"/>
                    </trans-unit>
                </xsl:when>
                <xsl:otherwise>
                    <trans-unit id="{concat(local-name(),'-',(count(preceding-sibling::td) + 1))}">
                            <source>
                                <xsl:apply-templates />
                            </source>
                            <target>
                                <xsl:apply-templates />
                            </target>
                        </trans-unit>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>

ВЫВОД

<trans-unit id="td-1">
    <source>All Label Subcategories</source>
    <target>target empty</target>
</trans-unit>
<trans-unit id="td-2">
    <source>some text</source>
    <target>some text</target>
</trans-unit>
person Joel M. Lamsen    schedule 28.05.2014