I have the following structure of xml data to transform:
<root>
<main1>
<page>
<text-body>
<title>K1</title>
<subtitle>Text</subtitle>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
</page>
<page>
<text-body>
<title>K2</title>
<subtitel>Text</subtitel>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
</page>
<page>
<text-body>
<title>K3</title>
<subtitel>Text</subtitel>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
</page>
</main1>
<main2>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body>
<text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body>
<text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
</main2>
</root>
And I need to replace the data in main1/text-body with titles K1, K2, K3 with the data in main2/text-body, but keep the other text-body elements in main1. The output should look like this:
<root>
<main1>
<page>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
</page>
<page>
<text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
</page>
<page>
<text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
<text-body>
<title>Text</title>
<subtitel>Text</subtitel>
</text-body>
</page>
</main1>
<main2>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body>
<text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body>
<text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
</main2>
</root>
I have the following xsl-code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="main1/page/text-body">
<xsl:param name="count" select="title/substring(.,2,1)"/>
<xsl:if test="title/substring(.,1,1)='K'">
<xsl:copy-of select="/root/main2/text-body[$count]"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I try to select the number in the title and to check, if there is a K in the text. But it doesn´t work. And I have no idea how to keep the other text-body elements. Here is the current output:
<main1>
<page>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body><text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body><text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
</page>
<page>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body><text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body><text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
</page>
<page>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body><text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body><text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
</page>
</main1>
<main2>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body>
<text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body>
<text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
</main2>
</root>
Please help!