working on it ...

Filters

Explore Public Snippets

Sort by

Found 336 snippets

    public by cghersi  185245  1  6  1

    Use the Raw HTML content in XSLT transformations

    Usage of copy-of element allows to get the exact content of an XML node and put it as raw HTML, so that e.g. are not stripped out as instead happens with
    <myXmlNode>
    	<question>
    	Some text <br/> separated < br/> by new lines
    	</question>
    </myXmlNode>
    
    <span>
    	<xsl:copy-of select="question"/>
    </span>

    public by Percival  3220  0  6  1

    Usage of xsl:if operator in XSLT

    Simple yet effective example of xsl:if condition in templates and transformations
    <span style="color:ff8500;">
      <xsl:if test="@RefersTo = 'SomeValue'">
        <xsl:value-of select="@MyAttr"/>
      </xsl:if>
    </span>

    public by Crater  2805  0  6  0

    Usage of choose operator in XSLT

    This is a simple yet common use of choose operator for templates
    <xsl:choose>
      <xsl:when test="@RefersTo = 'Value'"> some normal text</xsl:when>
      <xsl:otherwise>
        <a style="text-decoration: none;">
          <xsl:attribute name="href">
            <xsl:value-of select="@myurl"/>
          </xsl:attribute>
          <span> another text in the hyperlink</span>
        </a>
      </xsl:otherwise>
    </xsl:choose>

    public by Casper  4039  0  7  3

    foreach construct in XSLT templates

    Simple example of a for each construct
    <xsl:for-each select="myXMLElem" >
      <div>
        <xsl:text disable-output-escaping="yes">&amp;raquo;&amp;nbsp;</xsl:text>
        <a style="text-decoration: none; margin-left:0.5em;">
          <xsl:attribute name="href">
            <xsl:value-of select="@myAttrInXMLElem"/>
          </xsl:attribute>
          <span><xsl:value-of select="@anotherAttr"/></span>
        </a>
      </div>
    </xsl:for-each>

    external by Michael Kay  1  0  2  0

    Find duplicate value and remove from select in XSLT 2.0

    I have a code that find all the child node(within a same parent) and combine the value. &lt;xsl:template name="GetAllCompanys"&gt; &lt;xsl:param name = "currZOFLFA1"/&gt; &lt;xsl:for-each select="$currZOFLFA1/*:ZRTLFA1"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="position() = 1"&gt; &lt;xsl:value-of selec
    /* 
    Firstly your current code can be simplified to:
     */
    <xsl:template name="GetAllCompanys">
        <xsl:param name = "currZOFLFA1"/>
        <xsl:value-of select="$currZOFLFA1/*:ZRTLFA1/*:BUKRS" separator=", "/>
    </xsl:template>
    
    
    /* 
    (Assuming the caller of the template doesn't mind getting the result back as a single text node rather than a sequence of text nodes).
    
    Then, to remove duplicates, you can simply do:
     */
    <xsl:template name="GetAllCompanys">
        <xsl:param name = "currZOFLFA1"/>
        <xsl:value-of select="distinct-values($currZOFLFA1/*:ZRTLFA1/*:BUKRS)"
                      separator=", "/>
    </xsl:template>
    
    
    /* 
    The distinct-values() function doesn't guarantee order of results; if that's a concern, you may want to use xsl:for-each-group instead.
     */
    

    external by Martin Honnen  1  0  2  0

    xslt 2.0 - get latest node from recursive structure

    I want to search for a specific node in a recursive nodes structure and read a value(s) from inside of latest one. The solution I've tried is to call template recursively with searching for a condition, then put result to a variable, then loop through a variable by for-each. For example, my.xml: &lt;test&gt; &lt;test&gt; &lt;test&gt;
    /* 
    It seems a simple path expression using last() will give you the result:
     */
    <xsl:value-of select="(//test[@result and @result != 'undefined'])[last()]/metadata/@data"/>
    
    
    /* 
    https://xsltfiddle.liberty-development.net/94AbWBo
    
    If you use a template and want to return a sequence of attribute nodes and also want to store them as a sequence you can access then you need to use the as attribute e.g. 
     */
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:template name="my_template">
            <xsl:param name="node"/>
            <xsl:if test="$node/@result and $node/@result != 'undefined'">
                <xsl:sequence select="./metadata/@data"/>
            </xsl:if>
            <xsl:for-each select="$node/test">
                <xsl:call-template name="my_template">
                    <xsl:with-param name="node" select="." />
                </xsl:call-template>
            </xsl:for-each>
        </xsl:template>
    
        <xsl:template match="/">
            <xsl:variable name="var" as="attribute()*">
                <xsl:call-template name="my_template">
                  <xsl:with-param name="node" select="."/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:value-of select="$var[last()]"/>
        </xsl:template>
    
    </xsl:stylesheet>
    
    
    /* 
    https://xsltfiddle.liberty-development.net/94AbWBo/2
     */
    

    external by StackOverflow  1  0  3  0

    Image resizing to fit on PDF page using Saxon HE Apache FOP XSL-FO XSLT 2.0

    We are transforming XML to PDF using Saxon HE/ Apache FOP (XSLT 2.0). I would like to resize some of the images down to fit on the page. I understand to use the content-height and content-width "scale-down-to-fit" or "scale-to-fit" attributes but the height and width of the image is not available in the XML. I need a way to access the image file di
    /* 
    Section 3.1 of the EXPath Binary module gives you an example of how to do exactly this:
    
    http://expath.org/spec/binary
    
    Saxon (PE and higher) implements this library. (For Saxon-HE, however, you're out of luck).
     */
    

    external by Martin Honnen  1  0  2  0

    XSLT 2.0 get distinct nodes names and preserve order

    I am trying to transform XML to CSV where each entry does not contain all values. The column order must be preserved. Initial file: &lt;?xml version='1.0' encoding='UTF-8'?&gt; &lt;data&gt; &lt;entry&gt; &lt;a&gt;FR&lt;/a&gt; &lt;b&gt;Dupont&lt;/b&gt; &lt;c&gt;123456&lt;/c&gt; &lt;d&gt;zzz&lt;/d&gt; &lt;f&gt;New Y
    /* 
    I would use the following approach:
     */
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
    
      <xsl:output method="text"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:variable name="cols" 
         as="xs:string*" 
         select="let $max-cols := max(data/entry/count(*))
                 return distinct-values(data/entry[count(*) = $max-cols]/*/local-name())"/>
    
      <xsl:template match="data">
          <xsl:value-of select="$cols" separator=";"/>
          <xsl:text>&#10;</xsl:text>
          <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="entry">
          <xsl:value-of 
            select="for $col in $cols
                    return string(*[local-name() = $col])" 
            separator=";"/>
          <xsl:text>&#10;</xsl:text>
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    /* 
    https://xsltfiddle.liberty-development.net/bFWR5Em/1
     */
    

    external by Ed Bangga  1  0  2  0

    XSLT 2.0 Sort by variable from another style sheet

    I need to create XML that is sorted by numeric values I pull from another XSLT, which I use as a cross reference. The below source XML (source.xml) has four alpha characters at Partner/Header/@whse. &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Partner partnerId="TradingPartner1"&gt; &lt;Header whse="NCCH" &gt; &lt;Contract claim
    /* 
    Apply sorting after getting the result of choose. What happen is you are still generating the xml output, but you are sorting it on the Header item level.
     */
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
        <xsl:include href="Duns_config.xslt"/> 
        <xsl:template match="Partner">
            <xsl:variable name="partial">
                <PartnerTemp partnerId="{./@partnerId}">
                    <xsl:apply-templates select="./Header" />
                </PartnerTemp>
            </xsl:variable>
            <xsl:apply-templates select="$partial"></xsl:apply-templates>
        </xsl:template>
        <xsl:template match="PartnerTemp">
            <Partner partnerId="{./@partnerId}">
                <xsl:perform-sort select="transaction">
                    <xsl:sort select="@varwhse"/>
                </xsl:perform-sort>
            </Partner>
        </xsl:template>
        <xsl:template match="Header">
            <xsl:variable name="headerDuns">
                <xsl:call-template name = "SHIPTODUNS">
                    <xsl:with-param name="Whse" select="./@whse" />
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="varWhse">           
                <xsl:value-of select="substring($headerDuns, 11, 3)" />
            </xsl:variable>
    
            <xsl:for-each select="current()">               
                <transaction varwhse="{$varWhse}">              
                    <duns number="{$headerDuns}" />
                </transaction>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    
    
    /* 
    
     */
    

    external by Tim C  1  0  2  0

    map in xslt 2.0 is not working while passing key-value from parameter?

    I am facing problem while fetch data from map: key : &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"&gt; &lt;xsl:output indent="yes"/&gt; &lt;xsl:variable name="apos"&gt;'&lt;/xsl:var
    /* 
    $pos contains a string of 3 characters in length; literally '3'.
    
    When you do $months('3') the apostrophes are to indicate the use of a string literal (as opposed to a number). By the string itself is just a single character 3.
    
    You need to change the declaration of $pos to this, so that then it gets set to a string literal containing just a single character of 3.
     */
    <xsl:variable name="pos" select="string($mnth)"/>
    
    
    /* 
    Also note, the map function is only available in XSLT 3.0. If you are using an XSLT 3.0 processor, like Saxon 9.8 HE, it will still process the map function even if you put version="2.0" in your spreadsheet.
     */
    
    • Public Snippets
    • Channels Snippets