简体   繁体   中英

XSLT copy child node if parent contains X and Y as child nodes

I have source html sth like this:

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

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

I am using xslt from xliffRoundTrip Tool , I am not sure I should past the whole xlst here , it opensource though . Part of the xslt that is responsible for this conversion and needs to modify is :

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

My intention is to pass custom data for target language , as on many occasions we need to revise target language and pass the target string for revision.

Edits I modified the xslt it but it copies twice e. g

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

Might work. Try stand-alone first, without the funky open source tool.

Given a source file like

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

and this stylesheet:

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

OUTPUT

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM