获取当前节点号的XSLT/XPath

tan*_*ita 2 xml xslt xpath

我有以下xml数据结构进行转换:

 <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>
Run Code Online (Sandbox Code Playgroud)

我需要将main/text-body中的数据替换为main2/text-body中的数据,但保留main1中的其他内容.输出应如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

我有以下xsl代码:

<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>
Run Code Online (Sandbox Code Playgroud)

我尝试获取main1/text-body的当前节点索引来填充main2的正确文本体.但它不起作用.这是当前的输出:

<?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>
Run Code Online (Sandbox Code Playgroud)

请帮忙!

Mad*_*sen 5

XPath和XSLT是基于1的索引.为了选择第一个项目,谓词过滤器会查找[1],而不是[0].

鉴于此,变量$count需要1添加到count()前一个兄弟选择中.此外,您应该计算text-body兄弟之前的元素,而不是所有/任何元素node()

<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::text-body)+1"/>
        <xsl:copy-of select="/root/main2/text-body[$count]"/>
    </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)