I have the following structure of xml data to transform:
<root>
<main1>
<text-body>
<title>A</title>
<subtitle>A</subtitle>
</text-body>
<!-- other stuff -->
<text-body>
<titel>Aa</titel>
<subtitel>Aa</subtitel>
</text-body>
<!-- other stuff -->
<text-body>
<titel>Aaa</titel>
<subtitel>Aaa</subtitel>
</text-body>
</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 main/text-body with the data in main2/text-body, but keep the other stuff in main1. The output should look like this:
<root>
<main1>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body>
<!-- other stuff -->
<text-body>
<title>C</title>
<subtitle>C</subtitle>
<body>C</body>
</text-body>
<!-- other stuff -->
<text-body>
<title>D</title>
<subtitle>D</subtitle>
<body>D</body>
</text-body>
</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/text-body">
<xsl:param name="count" select="count(preceding-sibling::node())"/>
<xsl:copy-of select="/root/main2/text-body[$count]"/>
</xsl:template>
</xsl:stylesheet>
I try to get the current node index of main1/text-body to fill in the right text-body of main2. But it doesn´t work. Here is the current output:
<?xml version="1.0" encoding="UTF-8"?><root>
<main1>
<text-body>
<title>B</title>
<subtitle>B</subtitle>
<body>B</body>
</text-body>
<!-- other stuff -->
<!-- other stuff -->
</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!
XPath and XSLT are 1-based indexes. In order to select the first item, your predicate filter would look for
[1], not[0].Given that, the variable
$countneeds to have1added to thecount()of the preceding-sibling selection. Also, you should be counting thetext-bodyelements that are preceding-siblings, not all/anynode()