使用 xsl 将元素从一个节点复制到另一个节点

问题描述 投票:0回答:1

我正在使用 xsl 使节点 B 的值引用节点 A 的值。因此,我定义了一个键来取值

2
。然后,我将值传递给 w:c
<xsl:copy-of select="key('title','w:t')"/>
输出不将值传递给
 
。如何将值 2 从
<w:t>2</w:t>
复制到
<w:c>2</w:c>
?另一个问题是,如何避免
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
添加到
w:c

Content3.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
  <w:t>2</w:t>
  <w:c></w:c>
</w:document>

format3.xls

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
     <xsl:key name="title" match="w:document" use="w:t"/>
     <xsl:template match="w:c"> 
                <xsl:copy>
                  <xsl:copy-of select="key('title','w:t')"/>
                </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

index.php

<?php
// Load the XML source
$xml = new DOMDocument;
$xml->load('content3.xml');

$xsl = new DOMDocument;
$xsl->load('format3.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

//echo $proc->transformToXML($xml);
$newDOM = $proc->transformToDoc($xml);
$newDOM->formatOutput = true;
$newDOM->save("newfile.xml")
?>

输出

<?xml version="1.0"?>

2

<w:c xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"/>

期待

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
    <w:t>2</w:t>
    <w:c>2</w:c>
</w:document>

尝试使用

xsl:value-of select=
来引用值2.

格式.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
 <w:t>2</w:t>
 <w:c><xsl:value-of select="w:document/w:t"/></w:c>
</w:document>
</xsl:stylesheet>

仅输出显示 2

<?xml version="1.0"?>

2
php xml xslt openxml
1个回答
0
投票

我已经添加

<xsl:template match="/">
并使用
xsl:value-of select="w:document/w:t"

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
 <xsl:template match="/">
 <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
   <w:t>2</w:t>
   <w:c><xsl:value-of select="w:document/w:t"/></w:c>
  </w:document>
 </xsl:template>
</xsl:stylesheet>

这可以产生欲望输出:

    <?xml version="1.0"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
  <w:t>2</w:t>
  <w:c>2</w:c>
</w:document>
© www.soinside.com 2019 - 2024. All rights reserved.