我正在尝试在Mac上使用XSLT和Saxon合并几个相同类型的节点。
使用到目前为止我发现的内容,我提出了以下建议...
简化版本是这样:
Input.xml
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>1</id>
<other>y</other>
<notarget>x</notarget>
<target>red</target>
<target>green</target>
<target>blue</target>
</product>
<product>
<id>2</id>
<other>y</other>
<notarget>x</notarget>
<target>red</target>
<target>orange</target>
<target>yellow</target>
</product>
<product>
<id>3</id>
<other>y</other>
<notarget>x</notarget>
<target>yellow</target>
<target>purple</target>
<target>green</target>
</product>
</products>
transform.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<products>
<product>
<id>1</id>
<other>y</other>
<notarget>x</notarget>
<target><xsl:value-of select="string-join(//target/text(), ',')" /></target>
</product>
</products>
</xsl:template>
</xsl:stylesheet>
当前output.xml
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>1</id>
<other>y</other>
<notarget>x</notarget>
<target>red,green,blue,red,orange,yellow,yellow,purple,green</target>
</product>
</products>
但是它将所有节点组合成一个'产品',我想要的是:
所需的output.xml
<products>
<product>
<id>1</id>
<other>y</other>
<notarget>x</notarget>
<target>red, green, blue</target>
</product>
<product>
<id>2</id>
<other>y</other>
<notarget>x</notarget>
<target>red, orange, yellow</target>
</product>
<product>
<id>3</id>
<other>y</other>
<notarget>x</notarget>
<target>yellow, purple, green</target>
</product>
</products>
我在终端中使用以下命令:
saxon -s:input.xml -xsl:transform.xsl -o:output.xml '!indent=yes'
有人能指出我正确的方向吗?谢谢
答案 0 :(得分:1)
怎么样:
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="/products">
<products>
<xsl:for-each select="product">
<product>
<xsl:copy-of select="* except target" />
<target>
<xsl:value-of select="target" separator=", "/>
</target>
</product>
</xsl:for-each>
</products>
</xsl:template>
</xsl:stylesheet>