A
A
AnotherAnkor2018-11-29 13:31:46
.NET
AnotherAnkor, 2018-11-29 13:31:46

How to dynamically add blocks or change field values ​​when using xslt?

On hand are examples of xml and it is not known where xslt came from.
I need to have a little more freedom in my actions and would like:

  • be able to create a given value as a parameter (I have this number of bank transactions)
  • be able to influence the content (debit and credit in these operations must converge)

I used to work with xslt saxon and a wrapper that I found on codeproject . But this bundle does an excellent job with template transformations, but not with the task I described (I did not find any mention of such a possibility anywhere).
Does it exist at all? And if you really need it, then what?
UPD (examples):
The input xml (will not help us in any way) tells us for which account to return transaction data.
Output xml:
<?xml version="1.0" encoding="UTF-8"?>
<Statement xmlns="http://ююю"
           xmlns:a="urn:iso:std:iso:20022:tech:xsd:camt.053.001.04">
   <acceptDate>2018-11-28T13:23:43</acceptDate>
   <account>88888888888888888888</account>
   <bankBIC>5555555555</bankBIC>
   <creditReturn>1600000.00</creditReturn>
   <creditReturnNat>1600000.00</creditReturnNat>
   <currCode>RUB</currCode>
   <debetReturn>0.00</debetReturn>
   <debetReturnNat>0.00</debetReturnNat>
   <externalId>ABC59913</externalId>
   <docDate>2018-11-28T13:23:43</docDate>
   <fromDate>2018-11-28T00:00:00</fromDate>
   <isFinal>1</isFinal>
   <toDate>2018-11-28T00:00:00</toDate>
   <inboundBalance>2296351.83</inboundBalance>
   <inboundBalanceNat/>
   <outboundBalance>3896351.83</outboundBalance>
   <outboundBalanceNat/>
   <operations>
      <Operation>
         ....
      </Operation>
   </operations>
</Statement>

The output is formed based on xslt 2.0
The Operation block should be repeated an arbitrary number of times, and not as in the xslt provided to me (which I cannot provide due to nda) - 4 times. The individual fields in the Operation must also be filled in by me.
That is, I need to influence the number of operations and the values ​​of the fields in the operations.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Fov, 2018-11-29
@AnotherAnkor

Once for the test, you can write the same type of data
data.xml

<?xml version="1.0"?>
<root>
<foo/>
</root>

proc.xsl with recursion
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <operations>
            <xsl:call-template name="operation">
                <xsl:with-param name="counter" select="5"/>
            </xsl:call-template>
        </operations>
    </xsl:template>

    <xsl:template name="operation">
        <xsl:param name="counter" select="1"/>

        <operation>
            <name>
                <xsl:text>Ivan</xsl:text>
                <xsl:value-of select="$counter"/>
            </name>
            <price>
                <xsl:value-of select="100 * $counter"/>
            </price>
        </operation>

        <xsl:if test="$counter > 0">
            <xsl:call-template name="operation">
                <xsl:with-param name="counter" select="$counter - 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

xsltproc proc.xsl data.xml > result.xml
cat result.xml

<?xml version="1.0"?>
<operations>
  <operation>
    <name>Ivan5</name>
    <price>500</price>
  </operation>
  <operation>
    <name>Ivan4</name>
    <price>400</price>
  </operation>
  <operation>
    <name>Ivan3</name>
    <price>300</price>
  </operation>
  <operation>
    <name>Ivan2</name>
    <price>200</price>
  </operation>
  <operation>
    <name>Ivan1</name>
    <price>100</price>
  </operation>
  <operation>
    <name>Ivan0</name>
    <price>0</price>
  </operation>
</operations>

or for the second version on
proc.xsl cycles
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <operations>
            <xsl:for-each select="1 to 10">
                <operation>
                    <name>
                        <xsl:text>Ivan</xsl:text>
                        <xsl:value-of select="position()"/>
                    </name>
                    <price>
                        <xsl:value-of select="100 * position()"/>
                    </price>
                </operation>
            </xsl:for-each>
        </operations>
    </xsl:template>
</xsl:stylesheet>

saxonb-xslt -s:data.xml -xsl:proc.xsl -o:result.xml
cat result.xml

<?xml version="1.0" encoding="UTF-8"?>
<operations>
   <operation>
      <name>Ivan1</name>
      <price>100</price>
   </operation>
   <operation>
      <name>Ivan2</name>
      <price>200</price>
   </operation>
   <operation>
      <name>Ivan3</name>
      <price>300</price>
   </operation>
   <operation>
      <name>Ivan4</name>
      <price>400</price>
   </operation>
   <operation>
      <name>Ivan5</name>
      <price>500</price>
   </operation>
   <operation>
      <name>Ivan6</name>
      <price>600</price>
   </operation>
   <operation>
      <name>Ivan7</name>
      <price>700</price>
   </operation>
   <operation>
      <name>Ivan8</name>
      <price>800</price>
   </operation>
   <operation>
      <name>Ivan9</name>
      <price>900</price>
   </operation>
   <operation>
      <name>Ivan10</name>
      <price>1000</price>
   </operation>
</operations

This is it? So you wanted to generate them?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question