Показаны сообщения с ярлыком xslt. Показать все сообщения
Показаны сообщения с ярлыком xslt. Показать все сообщения

пятница, 18 декабря 2015 г.

XSLT 1.0 Возведение в степень

<xsl:template name="power">
        <xsl:param name="base" />
        <xsl:param name="power"/>
        <xsl:param name="result" select="1" />
        <xsl:choose>
            <xsl:when test="$power = 0">
                <xsl:value-of select="$result" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="power">
                    <xsl:with-param name="base" select="$base" />
                    <xsl:with-param name="power" select="$power - 1" />
                    <xsl:with-param name="result" select="$result * $base" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>



Как использовать:

  <xsl:variable name="pw">
     <xsl:call-template name="power">
                <xsl:with-param name="base">10</xsl:with-param>    
                 <xsl:with-param name="power" select="2"/>                                  
                 </xsl:call-template>
                </xsl:variable>
  <xsl:value-of select="$pw"/>

четверг, 10 декабря 2015 г.

XSLT. Из атрибутов в элементы и обратно

Из атрибутов в элементы:

<xsl:for-each select="@*">
         <xsl:element name="{name()}"><xsl:value-of select="."/></xsl:element>   
</xsl:for-each>      

Из элементов в атрибуты:


<xsl:for-each select="*">
   <xsl:attribute name="{name()}">
     <xsl:value-of select="text()"/>
   </xsl:attribute>                            
</xsl:for-each>                

среда, 5 августа 2015 г.

XML to Single Element/XML to String

Could you pls tell me in detail about how to achieve One whole xml into one string element. I am new to all this and have not used xslt or java mapping before.

Answer:

u can use XSLT mapping for inserting the complete input xml file in to string on output.

This is the sample code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<input_tag>
<xsl:text disable-output-escaping="yes"><![CDATA[><![CDATA[]]></xsl:text>
<xsl:copy-of select="output_tag/string_name"/>
<xsl:text disable-output-escaping="yes"><![CDATA[]]]]></xsl:text>
<xsl:text disable-output-escaping="yes"><![CDATA[>]]></xsl:text>
</input_tag>
</xsl:template>
</xsl:stylesheet>.

or u can use java mapping also
below is the sample code:
//package com.xi.javamapping;
import java.io.DataInputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;

public class XMLToString implements StreamTransformation {
     public void setParameter(Map arg0) {}
     public void execute(InputStream arg0, OutputStream arg1)
          throws StreamTransformationException {
          try {
               DataInputStream dataInputStream = new DataInputStream(arg0);
               StringBuffer sbr = new StringBuffer();
               String str = null;
               while ((str = dataInputStream.readUTF()) != null) {
                    String str1 = str.trim();
                    sbr.append(str1);
                    String string = new String(sbr);
                    byte[] b = string.getBytes();
                    arg1.write(b);
               }
          } catch (IOException e) {
               e.printStackTrace();
          }
     }
      
      
}

Source


 

понедельник, 27 июля 2015 г.

Xsl-if example

Исходный XML

<?xml version="1.0" encoding="UTF-8"?>
<Structure>
 <Food name="Яблоко">
   <IsFruit>true</IsFruit>
 </Food>
 <Food name="Картофель">
  <IsFruit>false</IsFruit>
   </Food>    
</Structure>
 
 
Правило преобразования 

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 
        <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
 
        <xsl:template match="/">
            <Structure><xsl:apply-templates select="//Food" /></Structure>      
        </xsl:template>
 
        <xsl:template match="Food">        
          <xsl:if test="IsFruit='true'"><Fruit name="{@name}" ></Fruit> 
          </xsl:if> 
 
          <xsl:if test="IsFruit='false'"><Vegan name="{@name}" ></Vegan> 
          </xsl:if>
        </xsl:template>
 
    </xsl:stylesheet>