How to ignore a group of elements if a specific node does not exist XSLT -



How to ignore a group of elements if a specific node does not exist XSLT -

i'm new xslt , i'm trying transform

<?xml version="1.0" encoding="utf-8"?> <file> <files> <segmentpair> <source>2. als vertreter der xxxx ein professor der materialwissenschaft oder der engineering science science und</source> <target>2. representative of university of xxxxx, professor of materials science or engineering science science</target> <comments> <comment>[xxx 09.01.2014 15:09:23] comment on translation</comment> </comments> </segmentpair> </files> </file>

into

<feedback>[xxx 09.01.2014 15:09:23] comment on translation</feedback> <source>2. als vertreter der xxxx ein professor der materialwissenschaft oder der engineering science science und</source> <target>2. representative of university of xxxxx, professor of materials science or engineering science science</target>

i'm not sure utilize not display target , source elements if no comments present. each , if? code thought test whether node exists don't think right way , doesn't work.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fo="http://www.w3.org/1999/xsl/format"> <xml:output method="xml" version="1.0" indent="yes"/> <xsl:template match="/"> <document> <xsl:for-each select="/files/file/segmentpair"> <xsl:if test="comments"> <feedback> <xsl:value-of select="comments/comment"/> </feedback> </xsl:if> </xsl:for-each> </document> </xsl:template> </xsl:stylesheet>

i grateful help, i've read many examples here i've got point can't work out efficient way, think missing out on simpler xslt 2.0.

thanks d.

i guessing want like:

<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:template match="/"> <document> <xsl:for-each select="file/files/segmentpair[comments/comment]"> <xsl:apply-templates select="comments/comment"/> <xsl:copy-of select="source|target"/> </xsl:for-each> </document> </xsl:template> <xsl:template match="comment"> <feedback> <xsl:value-of select="."/> </feedback> </xsl:template> </xsl:stylesheet>

applied illustration input, result be:

<?xml version="1.0" encoding="utf-8"?> <document> <feedback>[xxx 09.01.2014 15:09:23] comment on translation</feedback> <source>2. als vertreter der xxxx ein professor der materialwissenschaft oder der engineering science science und</source> <target>2. representative of university of xxxxx, professor of materials science or engineering science science</target> </document>

if no comment element present, result be:

<?xml version="1.0" encoding="utf-8"?> <document/>

if multiple comment elements found, each output feedback element.

note xml case-sensitive: comment not same thing comment.

xslt

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -