Xml 带有子字符串的xslt。我无法获得正确的输出

Xml 带有子字符串的xslt。我无法获得正确的输出,xml,xslt,Xml,Xslt,我已返回XSLT以获取输出。如果有任何错误,请更正此xslt <?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <item> <itemSale> <xsl:if test="itemSale='abed' and itemsea

我已返回XSLT以获取输出。如果有任何错误,请更正此xslt

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<item>
<itemSale>
<xsl:if test="itemSale='abed' and itemsearch='bra'">
<xsl:value-of select="substring(itemSale,1,2)">
</xsl:value-of>
</xsl:if>
</itemSale>
</item>
</xsl:template>
</xsl:stylesheet> 

我想要的输出xml是

<?xml version="1.0" encoding="UTF-8"?>
<item>
<itemSale>ab</itemSale>
</item>

ab
输入xml进行测试

<?xml version="1.0"?>
<item>
<itemSale>abed</itemSale>
<itemsearch>bra</itemsearch>
</item>

阿贝德
胸罩
但我得到的输出是xml

<?xml version="1.0" encoding="UTF-8"?>
<item>
<itemSale></itemSale>
</item>

您的模板与
/
(文档根)匹配,并且该模板的唯一子元素是
itemSale
itemsearch
不是根节点的子节点,因此
itemSale
生成0个节点,并且
itemSale='abed'
将始终为false

这里有两个主要选项:

  • 改为匹配文档元素(我建议这样做):
  • 
    
  • 使用元素的完整路径:
  • 
    
    但我还是发现,我已经尝试了您提供给我的第二个解决方案。JLRishe的答案是正确的,并且使用xsltproc为我工作。您是否未经修改就运行了提供的XSL?
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="/*">  <!--  here -->
        <item>
            <itemSale>
                <xsl:if test="itemSale='abed' and itemsearch='bra'">
                    <xsl:value-of select="substring(itemSale, 1, 2)" />
                </xsl:if>
            </itemSale>
        </item>
      </xsl:template>
    </xsl:stylesheet>
    
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="/">
        <item>
            <itemSale>
                <xsl:if test="item/itemSale='abed' and item/itemsearch='bra'">
                    <xsl:value-of select="substring(item/itemSale, 1, 2)" />
                </xsl:if>
            </itemSale>
        </item>
      </xsl:template>
    </xsl:stylesheet>