我有两个xml文件
file1.xml
<?xml version="1.0" encoding="UTF-8"?>
<tv>
...
<programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="A">
<title>A</title>
<sub-title>C</sub-title>
<desc>F</desc>
</programme>
...
<programme start="20200814090000 +0000" stop="20200814093000 +0000" channel="A">
<title>B</title>
<sub-title>D</sub-title>
<desc>E</desc>
</programme>
...
</tv>file2.xml
<?xml version="1.0" encoding="UTF-8"?>
<tv>
...
<programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="A">
<title>G</title>
<sub-title>C</sub-title>
<desc>H</desc>
<episode-num system="onscreen">S9 E13</episode-num>
</programme>
...
<programme start="20200814090000 +0000" stop="20200814093000 +0000" channel="A">
<title>K</title>
<sub-title>L</sub-title>
<desc>M</desc>
<episode-num system="onscreen">S3 E2</episode-num>
</programme>
...
</tv>我想要一个xslt 2模板来获得一个新文件。
file3.xml
<?xml version="1.0" encoding="UTF-8"?>
<tv>
...
<programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="A">
<title>A (G)</title>
<sub-title>C</sub-title>
<desc>F (H)</desc>
<episode-num system="onscreen">S9 E13</episode-num>
</programme>
...
<programme start="20200814090000 +0000" stop="20200814093000 +0000" channel="A">
<title>B (K)</title>
<sub-title>D (L)</sub-title>
<desc>E (M)</desc>
<episode-num system="onscreen">S3 E2</episode-num>
</programme>
...
</tv>我做了一些实验,但没有达到预期的结果。任何帮助都将不胜感激。
编辑以提高精度
当程序属性与每个文件相同时:
中。
发布于 2020-08-16 06:36:38
在XSLT 3中,也许for-each-pair函数可以帮助:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:param name="doc2">
<tv>
<channel id="Discovery">
<display-name lang="el">Discovery</display-name>
</channel>
<programme start="20200814040000 +0000" stop="20200814050000 +0000" channel="Discovery">
<title lang="el">Wheeler Dealers</title>
<sub-title lang="el">BMW Isetta</sub-title>
<desc lang="el">Mike tracks down an Isetta Bubble. </desc>
<episode-num system="onscreen">S9 E13</episode-num>
</programme>
</tv>
</xsl:param>
<xsl:output indent="yes"/>
<xsl:function name="mf:merge-pair">
<xsl:param name="programme1"/>
<xsl:param name="programme2"/>
<xsl:if test="deep-equal($programme1/@*, $programme2/@*)">
<xsl:copy select="$programme1">
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="$programme1/*, $programme2/*" composite="yes" group-by="node-name(), @*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="head(current-group()), tail(current-group()) ! ('(' || . || ')')"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:if>
</xsl:function>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="tv">
<xsl:copy>
<xsl:apply-templates select="@*, channel"/>
<xsl:sequence
select="for-each-pair(programme, $doc2/tv/programme, mf:merge-pair#2)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>在上面的例子中,我将第二个文档内联为完整和自我约束,但当然,在实际应用程序中,您可以使用例如<xsl:param name="doc2" select="doc('input2.xml')"/>。
XSLT 3,每对一对,可与撒克逊10所有版本,或商业版9.8或9.9版的撒克逊,或在撒克逊-JS 2为Node.js或浏览器。
至于你的评论,你似乎已经编辑了样本,现在看来,像BMW Isetta (BMW Isetta)这样的重复内容应该被删除,这样你就可以改变了。
<xsl:value-of select="head(current-group()), tail(current-group()) ! ('(' || . || ')')"/>至
<xsl:value-of select="let $values := distinct-values(current-group()) return (head(
$values), tail($values)! ('(' || . || ')'))"/>为我输出您编辑的样本和Saxon HE 10.1是
<tv>
<programme start="20200814040000 +0000"
stop="20200814050000 +0000"
channel="A">
<title>A (G)</title>
<sub-title>C</sub-title>
<desc>F (H)</desc>
<episode-num system="onscreen">S9 E13</episode-num>
</programme>
<programme start="20200814090000 +0000"
stop="20200814093000 +0000"
channel="A">
<title>B (K)</title>
<sub-title>D (L)</sub-title>
<desc>E (M)</desc>
<episode-num system="onscreen">S3 E2</episode-num>
</programme>
</tv>完整样式表是
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:param name="doc2" select="doc('file2.xml')"/>
<xsl:output indent="yes"/>
<xsl:function name="mf:merge-pair">
<xsl:param name="programme1"/>
<xsl:param name="programme2"/>
<xsl:if test="deep-equal($programme1/@*, $programme2/@*)">
<xsl:copy select="$programme1">
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="$programme1/*, $programme2/*" composite="yes" group-by="node-name(), @*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="let $values := distinct-values(current-group()) return (head(
$values), tail($values)! ('(' || . || ')'))"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:if>
</xsl:function>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="tv">
<xsl:copy>
<xsl:apply-templates select="@*, channel"/>
<xsl:sequence
select="for-each-pair(programme, $doc2/tv/programme, mf:merge-pair#2)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>发布于 2020-08-16 08:48:01
我会做这样的事情:
<xsl:variable name="file1" select="doc('file1.xml')"/>
<xsl:variable name="file2" select="doc('file2.xml')"/>
<xsl:template name="xsl:initial-template">
<tv>
<xsl:copy-of select="$file1/tv/channel"/>
<xsl:for-each-group select="($file1|file2)/tv/programme"
group-by="@stop, @start, @channel" composite="yes">
<xsl:for-each-group select="*" group-by="node-name()">
<xsl:element name="{name()}">
<xsl:copy-of select="current-group()/@*"/>
<xsl:value-of select="current-group()[1]"/>
<xsl:for-each select="current-group()[2]">
<xsl:value-of select="'(', ., ')'"/>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:for-each-group>
</tv>
</xsl:template>还没测试过。
https://stackoverflow.com/questions/63430231
复制相似问题