Xslt concatenate text from nodes

I have an xml file that looks like this:

<args>
  <sometag value="abc" />
  <anothertag value="def" />
  <atag value="blah" />
</args>

Keep in mind that the tag names inside args can be called anything (I don’t know in advance) Now I have this xml file, which is stored in the $ data variable, which I loaded by calling document () in the xslt stylesheet (this is not support data for xslt file)

I want to take this data and produce the following output: sometag = ABC & anothertag = Protection & ATAG = blah

so (a very simplified version looks like this:

<xsl:template>
 <xsl:variable name="data"  select="document('/path/to/xml')" />

  <xsl:call-template name='build_string'>
    <xsl:with-param name='data' select='$data' />

  </xsl:call-template>

</xsl:template>

<!-- here is where i need help -->
<xsl:template name="build_string">
  <xsl:param name='data'>
  <xsl:value-of select='name($data/.)' />=<xsl:value-of select='$data/@value' />

  <xsl:if test='$data/following-sibling::node()'>
    <xsl:text>&#38;</xsl:text>
    <xsl:call-template name="build_str">
     <xsl:with-param name="data" select='$nodes/following-sibling::node()' />
    </xsl:call-template>
  </xsl:if>


</xsl:template>

This almost works, but also prints text nodes from the input file, and I don't want to match text nodes.

+3
source share
2 answers

:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*/*">
  <xsl:value-of select="concat(name(),'=',@value)"/>

  <xsl:if test="not(position()=last())">
    <xsl:text>&amp;</xsl:text>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

XML-:

<args>
  <sometag value="abc"/>
  <anothertag value="def"/>
  <atag value="blah"/>
</args>

, :

sometag=abc&anothertag=def&atag=blah
+14

, for-each. , . , , ( , , ). ( )

<xsl:template name='string_builder'>
    <xsl:param name='data' />
    <xsl:param name='separator' />        
    <xsl:for-each select='$data/*'>
        <xsl:value-of select='name()'/>=<xsl:value-of select='@value'/>
        <xsl:if test='position() != last()'>
           <xsl:value-of select='$separator'/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
+3

Source: https://habr.com/ru/post/1771832/


All Articles