私は近似するxmlを持っています:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<num>Book 1.</num>
<head> Title</head>
<chapter>
<num>1.</num>
<head> The Begining</head>
<p>content</p>
</chapter>
<num>12. </num><p>we want that number untouched</p>
<chapter>
<num>2.</num>
<head> The Middle</head>
<p>content</p>
</chapter>
<head>Heads Occur</head><p>we want that head untouched</p>
</book>
<num>
a の直後に a が続く場合<head>
、2 つをマージしたいと思います。私はこの xsl を使用して、ある程度の成功を収めましたが、すべてのユースケースではありません。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="num[following-sibling::head]">
<mergedhead>
<xsl:apply-templates select="node()|@*"/>
<xsl:value-of select="following-sibling::head"/>
</mergedhead>
</xsl:template>
<!-- keep the old head from showing up in the new output-->
<xsl:template match="head[preceding-sibling::num]"/>
</xsl:stylesheet>
following::sibling
機能しpreceding::sibling
ますが、すべてのユースケースではありません。時々それらは引き込まれ<num>
、<head>
それは互いに直接隣り合っていません. 私の欠陥のある XSL の出力:
<?xml version="1.0" encoding="UTF-8"?>
<book>
<mergedhead>Book 1. Title</mergedhead>
<chapter>
<mergedhead>1. The Begining</mergedhead>
<p>content</p>
</chapter>
<mergedhead>12. Heads Occur</mergedhead><p>we want that number untouched</p>
<chapter>
<mergedhead>2. The Middle</mergedhead>
<p>content</p>
</chapter>
<p>we want that head untouched</p>
</book>
そのままにしておきたい #12 と、同じくそのままにしておきたい 'heads occur' をマージしたことがわかります。間に他のノードがあっても、それらが兄弟であるためだと私は知っています。私が望む答えは にあると思いますposition()
。しかし、私はそれで成功していません。
参考までに、目的のアウトは次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<book>
<mergedhead>Book 1. Title</mergedhead>
<chapter>
<mergedhead>1. The Begining</mergedhead>
<p>content</p>
</chapter>
<num>12. </num><p>we want that number untouched</p>
<chapter>
<mergedhead>2. The Middle</mergedhead>
<p>content</p>
</chapter>
<head>Heads Occur</head><p>we want that head untouched</p>
</book>