XSLT – Comparing 2 documents for matching parent and child elements

If I have the following two XML docs:

untranslated file:

 <body>
     <trans-unit id="Id a" maxwidth="240" size-unit="char">
                <source>original a</source>
     </trans-unit>
     <trans-unit id="Id b" maxwidth="240" size-unit="char">
                <source>original b</source>
     </trans-unit>
     <trans-unit id="Id c" maxwidth="240" size-unit="char">
                <source>original c</source>
     </trans-unit>
 </body>

translated file:

 <body>
     <trans-unit id="Id a" maxwidth="240" size-unit="char">
                <source>original a</source>
     </trans-unit>
     <trans-unit id="Id b" maxwidth="240" size-unit="char">
                <source>translated b</source>
     </trans-unit>
 </body>

I want to be able to output only the items where the source is different between the two. In this case the trans-unit ‘Id a’ is untranslated (the source is the same between both docs) so I want to exclude it. I only want to see the trans-unit ‘Id b’ from the second translated file because the source element contents is different.

Also, I cannot guarantee that both trans-units are in each file, as per ‘Id c’.

Final output document:

 <body>
     <trans-unit id="Id b" maxwidth="240" size-unit="char">
                <source>translated b</source>
     </trans-unit>
 </body>

I don’t have much experience with XSL, I am assuming this will involve holding one of the document’s contents in a variable and performing tests against it but I could be incorrect

<?xml version="1.0" encoding="utf-8"?>
<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:variable name="mytranslatedfile" select="document('translatedfile.xml')" />
<xsl:variable name="translated" select="$mytranslatedfile/trans-unit/id" />

<xsl:template match="trans-unit/source">
     <xsl:choose>

<!----- if the id is the same between the documents ----->

     <xsl:when test="$translated[ancestor::trans-unit[1]/@id]">

<!----- add an additional test here to compare the source? unsure how to do this ----->

<!----- I want to output the trans-unit element and child source element here ----->
     <P> <xsl:value-of select="."/> </P>

     </xsl:when>
     </xsl:choose>
</xsl:template>

</xsl:stylesheet>

1

With XSLT 3 and keys (second document is inlined for self containedness of example but could be a <xsl:param name="translation" select="doc('translations.xml')"/> instead:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all">
  
  <xsl:key name="translated" match="trans-unit" use="@id"/>
  
  <xsl:key name="source" match="trans-unit" composite="yes" use="@id, source"/>
  
  <xsl:variable name="main" select="/"/>

  <xsl:template match="body">
    <xsl:copy>
      <xsl:copy-of select="trans-unit/key('translated', @id, $translation)[not(key('source', (@id, source), $main))]"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:param name="translation">
     <body>
         <trans-unit id="Id a" maxwidth="240" size-unit="char">
                    <source>original a</source>
         </trans-unit>
         <trans-unit id="Id b" maxwidth="240" size-unit="char">
                    <source>translated b</source>
         </trans-unit>
     </body>    
  </xsl:param>
  
</xsl:stylesheet>

Example fiddle is here.

0

There is probably a more elegant way of doing this, but this should work:

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

<xsl:param name="translatedfile" select="document('translatedfile.xml')"/>

<xsl:template match="/body">
    <xsl:copy>
        <xsl:for-each select="trans-unit">
            <xsl:variable name="translation" select="$translatedfile/body/trans-unit[@id = current()/@id]" />
            <xsl:if test="source != $translation/source">
                <xsl:copy-of select="$translation"/>
            </xsl:if>   
        </xsl:for-each> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This is assuming the untranslated file is the XML input to the XSL transformation.

In XSLT 2.0 or higher, you could do simply:

<xsl:stylesheet version="2.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:param name="translatedfile" select="document('translatedfile.xml')"/>

<xsl:key name="k1" match="trans-unit" use="@id"/>

<xsl:template match="/body">
    <xsl:copy>
        <xsl:for-each select="trans-unit">
            <xsl:copy-of select="key('k1', @id, $translatedfile)[source != current()/source]"/>
        </xsl:for-each> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật