Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck existence of child node and applying templates
    primarykey
    data
    text
    <p>I have the following simplified XML structure:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;ExportData&gt; &lt;TransportHeader&gt; &lt;Timestamp&gt;2011-01-16 06:00:33&lt;/Timestamp&gt; &lt;From&gt; &lt;Name&gt;DynamicExport&lt;/Name&gt; &lt;Version&gt;1.&lt;/Version&gt; &lt;/From&gt; &lt;MessageId&gt;d7b5c5b69a83&lt;/MessageId&gt; &lt;/TransportHeader&gt; &lt;ExportConfig&gt; &lt;DateTimeFormat&gt;yyyy-MM-dd HH:mm:ss&lt;/DateTimeFormat&gt; &lt;DecimalSymbol&gt;.&lt;/DecimalSymbol&gt; &lt;/ExportConfig&gt; &lt;DataSet&gt; &lt;Tables&gt; &lt;Table&gt; &lt;RH&gt;...&lt;/RH&gt; &lt;Rows&gt; &lt;R&gt;Data1&lt;/R&gt; &lt;R&gt;Data2&lt;/R&gt; &lt;R&gt;Data3&lt;/R&gt; &lt;R&gt;Data4&lt;/R&gt; &lt;R&gt;Data5&lt;/R&gt; &lt;/Rows&gt; &lt;/Table&gt; &lt;/Tables&gt; &lt;/DataSet&gt; &lt;/ExportData&gt; </code></pre> <p>I have to check if <code>&lt;R&gt;</code> elements exist or not. If no <code>&lt;R&gt;</code> elements exist the mapping has to be aborted, otherwise a <code>&lt;Line&gt;</code> element per <code>&lt;R&gt;</code> needs to be created.</p> <p>I came up with this solution which works perfectly so far:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;xsl:output encoding="ISO-8859-1" method="xml" indent="yes" /&gt; &lt;!-- suppress nodes that are not matched --&gt; &lt;xsl:template match="text() | @*"&gt; &lt;xsl:apply-templates select="text() | @*"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="not(ExportData/DataSet/Tables/Table/Rows/node())"&gt; &lt;xsl:message terminate="yes"&gt;No line items&lt;/xsl:message&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/ExportData/DataSet/Tables/Table/Rows"&gt; &lt;INVOIC02&gt; &lt;!-- apply LINE ITEMS template --&gt; &lt;xsl:apply-templates select="R"/&gt; &lt;/INVOIC02&gt; &lt;/xsl:template&gt; &lt;!-- Template creating LINE ITEMS --&gt; &lt;xsl:template match="R"&gt; &lt;Line&gt; &lt;elements&gt;&lt;/elements&gt; &lt;/Line&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>If there are <code>&lt;R&gt;</code> elements the output is this:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;INVOIC02&gt; &lt;Line&gt; &lt;elements/&gt; &lt;/Line&gt; &lt;Line&gt; &lt;elements/&gt; &lt;/Line&gt; &lt;Line&gt; &lt;elements/&gt; &lt;/Line&gt; &lt;Line&gt; &lt;elements/&gt; &lt;/Line&gt; &lt;Line&gt; &lt;elements/&gt; &lt;/Line&gt; &lt;/INVOIC02&gt; </code></pre> <p>If there is just <code>&lt;Rows/&gt;</code> and no <code>&lt;R&gt;</code>s the mapping is aborted.</p> <p>Now I have two questions:</p> <p>-Is my test for <code>&lt;R&gt;</code> elements robust: <code>test="not(ExportData/DataSet/Tables/Table/Rows/node())"</code> ?</p> <p>-I am using <code>&lt;xsl:apply-templates&gt;</code> to create the <code>&lt;Line&gt;</code> items instead of an <code>&lt;xsl:for-each&gt;</code> construct. Are my XPath expressions okay or could I make them better?</p> <p>Thank you for any response or suggestions to that.</p> <p>Best regards, Peter</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload