Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XSLT将两个xml元素组合成一个字符串?_Xml_Xslt - Fatal编程技术网

如何使用XSLT将两个xml元素组合成一个字符串?

如何使用XSLT将两个xml元素组合成一个字符串?,xml,xslt,Xml,Xslt,我的XML文件中有两个元素,我想在一个字符串中组合两个值。这是我的XSLT解决方案: <xsl:for-each select="housenumber/@value | housenumberletter/@value"> <houseinformation> <xsl:variable name="info" select="xs:string((housenumber| housenumberletter

我的XML文件中有两个元素,我想在一个字符串中组合两个值。这是我的XSLT解决方案:

 <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>

这是我在氧气中测试的结果:

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

我想要的预期结果如下:

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

我还尝试了concat功能,但没有成功

如何将XML中的两个元素组合成一个字符串

更新:

我的xml文件:

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

应使用
$
引用
$info
变量。否则,它会认为您正在尝试使用XPath访问名为
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>

或者避免使用变量,只需将该表达式内联到


给定XML示例,以下样式表:

XSLT2.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>

将返回:

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

如果不需要空格分隔符,请尝试:

<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>

或者简单地说:

<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>


谢谢您抽出时间!我已经尝试了你的第二种解决方案:
是的,很抱歉输入错误。我已经更正了答案中的代码,请发布一个包含输入的答案。-请注意,
将您置于
属性的上下文中;在此上下文中,指令
select=“xs:string((housenumber | housenumberletter)/@value)”
将永远不会选择任何内容。已添加我的XML文件
<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>