简体   繁体   中英

How to combine 2 xml elements in one string with XSLT?

I have two elements in my XML file and I want to combine 2 values in one string. This is my XSLT solution:

 <xsl:for-each select="housenumber/@value | housenumberletter/@value">
 <houseinformation>
 <xsl:variable name="info" select="xs:string((housenumber| housenumberletter)/@value)" />
 <valueString value="info"/>
 </houseinformation>
 </xsl:for-each>

This is my result when I test it in Oxygen:

<houseinformation>
<valueString value="info"/>
</houseinformation>
<houseinformation>
<valueString value="info"/>
</houseinformation>

Expected result that I want is like this:

<houseinformation>
<valueString value="4A"/>
</houseinformation>

I also tried the concat function but it didn't work.

How can I combine two elements from XML in one string?

UPDATE:

My xml file:

<xml>   
    <data>  
    <housenumber value="15"/>
    <houseletter value="A"/> 
    </data>
</xml> 

Given your XML example, the following stylesheet:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/xml">
    <root>
        <xsl:for-each select="data">
            <xsl:variable name="info" select="(housenumber | houseletter)/@value" />
            <houseinformation>
                <valueString value="{$info}"/>
            </houseinformation>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

will return:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <houseinformation>
      <valueString value="15 A"/>
   </houseinformation>
</root>

If you don't want the space separator, try:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/xml">
    <root>
        <xsl:for-each select="data">
            <xsl:variable name="info" select="(housenumber | houseletter)/@value" />
            <houseinformation>
                <valueString>
                      <xsl:attribute name="value" select="$info" separator=""/>
                </valueString>
            </houseinformation>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

or simply:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/xml">
    <root>
        <xsl:for-each select="data">
            <houseinformation>
                <valueString value="{concat(housenumber/@value, houseletter/@value)}"/>
            </houseinformation>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

The $info variable should be referred to with the $ . Otherwise, it thinks you are attempting to XPath to an element named info :

<xsl:for-each select="housenumber/@value | housenumberletter/@value">
  <houseinformation>
    <xsl:variable name="info" select="xs:string((housenumber| housenumberletter)/@value)" />
    <valueString value="$info"/>
   </houseinformation>
 </xsl:for-each>

Or avoid the variable and just put that expression inline inside of an attribute value template :

<xsl:for-each select="housenumber/@value | housenumberletter/@value">
  <houseinformation>
    <valueString value="{xs:string((housenumber| housenumberletter)/@value)}"/>
  </houseinformation>
</xsl:for-each>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM