我对XML数据的结构如下改造:XSLT/XPath的更换特定节点
<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>
我需要用头衔K1替换MAIN1 /文本主体中的数据,K2,K3与数据在main2/text-body中,但将其他文本主体元素保留在main1中。输出应该是这样的:
<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>
我有以下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/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>
我尽量选择在标题和检查的人数,如果在文本A K 。但它不起作用。而且我不知道如何保留其他文本元素。这里是当前输出:
<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>
请帮忙!
根据我的经验,像'吨itle/substring(。,2,1)'在XSLT 2.0中运行良好。什么反对这样做? –
你是对的。我愚蠢地使用XSLT1.0处理器来测试我的答案!我已经修改了我的答案,以删除有关错误的子字符串的位。 –
非常感谢! –