Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This transformation</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/"&gt; Abstract: &lt;xsl:text/&gt; &lt;xsl:for-each select="*/MT[starts-with(@N, 'Abstract')]"&gt; &lt;xsl:value-of select="@V"/&gt; &lt;/xsl:for-each&gt; Author: &lt;xsl:text/&gt; &lt;xsl:for-each select="*/MT[starts-with(@N, 'Author')]"&gt; &lt;xsl:value-of select="@V"/&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the provided XML document:</strong></p> <pre><code>&lt;R N="14" MIME="application/pdf"&gt; &lt;RK&gt;7&lt;/RK&gt; &lt;MT N="Abstract" V="Lorem Ipsum is simply dummy text of the printing " /&gt; &lt;MT N="Abstract1" V="and typesetting industry. Lorem Ipsum has been the industry's standard " /&gt; &lt;MT N="Author" V="Bernard Shaw;" /&gt; &lt;MT N="Author1" V="Mark Twain" /&gt; &lt;MT N="Abstract2" V="dummy text ever since the 1500s, when an unknown printer took a galley" /&gt; &lt;LANG&gt;en&lt;/LANG&gt; &lt;/R&gt; </code></pre> <p><strong>produces the wanted result:</strong></p> <pre><code>Abstract: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley Author: Bernard Shaw;Mark Twain </code></pre> <p><strong>Further refactoring</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/"&gt; Abstract: &lt;xsl:text/&gt; &lt;xsl:call-template name="concatAttributes"/&gt; Author: &lt;xsl:text/&gt; &lt;xsl:call-template name="concatAttributes"&gt; &lt;xsl:with-param name="pKeyStartString" select="'Author'"/&gt; &lt;/xsl:call-template&gt; &lt;/xsl:template&gt; &lt;xsl:template name="concatAttributes"&gt; &lt;xsl:param name="pKeyAttribName" select="'N'"/&gt; &lt;xsl:param name="pKeyStartString" select="'Abstract'"/&gt; &lt;xsl:param name="pValueAttribName" select="'V'"/&gt; &lt;xsl:for-each select= "*/MT[starts-with(@*[name()=$pKeyAttribName], $pKeyStartString)]"&gt; &lt;xsl:value-of select="@*[name()=$pValueAttribName]"/&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>A second refactoring (requested by the OP):</strong></p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:variable name="vAbstract"&gt; &lt;xsl:apply-templates mode="retrieve" select="//MT"/&gt; &lt;/xsl:variable&gt; &lt;xsl:variable name="vAuthors"&gt; &lt;xsl:apply-templates mode="retrieve" select="//MT"&gt; &lt;xsl:with-param name="pKeyStartString" select="'Author'"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:variable&gt; &lt;xsl:template match="/"&gt; Abstract: &lt;xsl:value-of select="$vAbstract"/&gt; Authors:: &lt;xsl:value-of select="$vAuthors"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="MT" mode="retrieve"&gt; &lt;xsl:param name="pKeyAttribName" select="'N'"/&gt; &lt;xsl:param name="pKeyStartString" select="'Abstract'"/&gt; &lt;xsl:param name="pValueAttribName" select="'V'"/&gt; &lt;xsl:if test="starts-with(@*[name()=$pKeyAttribName], $pKeyStartString)"&gt; &lt;xsl:value-of select="@*[name()=$pValueAttribName]"/&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>EDIT</strong>: The OP has requested that "all processing should be done within the template matching <code>MT</code>". While this is possible in simple cases (see @Alejandro's answer), doing all the processing in a single template matching <code>MT</code> opens big gaps of unknowns. For example, it may be required to process other <code>MT</code> elements in a different way and in this case such processing will not be done at all.</p> <p>In more complex cases (such as when the elements and attributes come in any order, but the output must be sorted -- Abstract1, Abstract2, ..., Abstract-N), then the sort has to be specified explicitly and this has to be outside the template matching <code>MT</code>. <strong>Therefore, in the general case, it is not possible to produce the required output with code only within the template matching <code>MT</code>.</strong></p> <p><strong>I would recommend that the single template matching <code>MT</code> be in a named mode</strong> and that it should be used in "pull style" -- applied explicitly by the caller.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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