XSLT剥离文本输出中的所有选项卡
愚蠢、简单的问题。当我输出文本时,它仍然会根据我的格式化/缩进XSL结构获取选项卡。如何指示转换器忽略样式表中的间距,同时保持样式表的格式整洁XSLT剥离文本输出中的所有选项卡,xslt,formatting,Xslt,Formatting,愚蠢、简单的问题。当我输出文本时,它仍然会根据我的格式化/缩进XSL结构获取选项卡。如何指示转换器忽略样式表中的间距,同时保持样式表的格式整洁 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="Foo/Bar"></xsl:apply-templates>
</xsl:template>
<xsl:template match="Bar">
<xsl:for-each select="AAA"><xsl:for-each select="BBB"><xsl:value-of select="Label"/>|<xsl:value-of select="Value"/><xsl:text> </xsl:text></xsl:for-each></xsl:for-each>
</xsl:template>
</xsl:stylesheet>
更新:
添加此选项并不能解决此问题:
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"></xsl:strip-space>
这是人为的,但您可以想象XML是这样的:
<Foo>
<Bar>
<AAA>
<BBB>
<Label>SomeLabel1</Label>
<Value>SomeValue1</Value>
</BBB>
<BBB>
<Label>SomeLabel2</Label>
<Value>SomeValue2</Value>
</BBB>
<BBB>
<Label>SomeLabel3</Label>
<Value>SomeValue3</Value>
</BBB>
</AAA>
</Bar>
</Foo>
您可以尝试用xsl:text包装所有当前文本节点。例如,试试这个
<xsl:for-each select="BBB">
<xsl:value-of select="Label"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="Value"/>
<xsl:text>|</xsl:text>
</xsl:for-each>
然后您将有一个特定的模板来匹配标签,在这里您不需要输出分隔符,还有一个更通用的模板匹配BBB的任何子级
这里的优先级需要确保标签与第一个模板匹配,而不是与通用模板匹配。当然,在这种情况下,您也不能在标签上应用模板,而只需为该模板应用xsl:value
此外,如果字段是按照它们在XML中的显示顺序输出的,则可以将for each简化为这一顺序
<xsl:for-each select="BBB">
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:for-each>
您发布的XSLT代码不会产生您声称的结果:@michael.hor257k Updated。在XSLtuts.NET上缩进是正确的,但是我在VisualStudio和NotoPosi++中运行了这一点,并且当XSLT是Tab-Dutin时,都将插入的选项卡放入输出。这是有效的,并且我认为这是部分解决方案。如果我有两个字段,效果很好,但实际上我必须连接大约50个字段。没有一种方法可以在不将concat应用于所有需要的内容的情况下普遍解决这个问题吗?此外,在每个标签之前仍然包含一个选项卡。@kakridge实际上,我必须连接大约50个字段。我们只能看你给我们看的东西。在您的代码中,您显式地调用每个字段,因此为每个这样的调用添加一个文本元素并不费力。或者,您可以构造一个for-each-field循环,并将文本元素插入其中一次。当然,这不是一项很大的工作,但我很懒,更喜欢忽略整个样式表中元素之外的所有空白的方法。我听说这是不可能的。谢谢大家,xsl:text元素是一个可以接受的答案。仍然令人失望的是,无法使用processing指令忽略样式表中的所有空白。也许strip空间应该做到这一点,但它不能与我正在使用的处理工具一起工作。
<xsl:for-each select="BBB">
<xsl:value-of select="Label"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="Value"/>
<xsl:text>|</xsl:text>
</xsl:for-each>
<xsl:for-each select="BBB">
<xsl:value-of select="concat(Label, '|')"/>
<xsl:value-of select="concat(Value, '|')"/>
</xsl:for-each>
<xsl:for-each select="BBB">
<xsl:value-of select="concat(Label, '|', Value, '|')"/>
</xsl:for-each>
<xsl:for-each select="BBB">
<xsl:apply-templates select="Label"/>
<xsl:apply-templates select="Value"/>
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:template match="BBB/Label" priority="1">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="BBB/*">
<xsl:text>|</xsl:text><xsl:value-of select="." />
</xsl:template>
<xsl:for-each select="BBB">
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:for-each>