将一个节点中的元素复制到另一个节点中,使用XSL。

huangapple go评论57阅读模式
英文:

copy element from one node to another using xsl

问题

以下是您要求的中文翻译部分:

"我正在使用XSL将节点B的值引用为节点A的值。因此,我定义了一个键来获取值2。然后,我将值传递给w:c,使用<xsl:copy-of select="key('title','w:t')"/>。输出未将值传递给<w:c>。如何将<w:t>2</w:t>的值复制到<w:c>2</w:c>?另一个问题是,如何避免将xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"添加到w:c中?

期望的输出如下:

<?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,但只显示2,这部分代码如下:

<?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,因为<xsl:value-of select="w:document/w:t"/>只会输出<w:t>2</w:t>的文本内容,不会包含其他标签。如果您希望复制整个<w:t>元素,您需要使用<xsl:copy-of>,就像您之前的代码示例中一样。

英文:

I am using xsl to make value at node B refere to value at node A. So, I define a key to take value 2. then, I pass in value to w:c with &lt;xsl:copy-of select=&quot;key(&#39;title&#39;,&#39;w:t&#39;)&quot;/&gt; Output not passing value to <w:c> </w:c>. How to copy value 2 from &lt;w:t&gt;2&lt;/w:t&gt; to &lt;w:c&gt;2&lt;/w:c&gt;? Another question is, how to avoid xmlns:w=&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot; added to w:c

Content3.xml

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

format3.xls

&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:w=&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot;&gt;
	 &lt;xsl:key name=&quot;title&quot; match=&quot;w:document&quot; use=&quot;w:t&quot;/&gt;
	 &lt;xsl:template match=&quot;w:c&quot;&gt; 
	 			&lt;xsl:copy&gt;
	 			  &lt;xsl:copy-of select=&quot;key(&#39;title&#39;,&#39;w:t&#39;)&quot;/&gt;
	 			&lt;/xsl:copy&gt;
	&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

index.php

&lt;?php
// Load the XML source
$xml = new DOMDocument;
$xml-&gt;load(&#39;content3.xml&#39;);

$xsl = new DOMDocument;
$xsl-&gt;load(&#39;format3.xsl&#39;);

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

//echo $proc-&gt;transformToXML($xml);
$newDOM = $proc-&gt;transformToDoc($xml);
$newDOM-&gt;formatOutput = true;
$newDOM-&gt;save(&quot;newfile.xml&quot;)
?&gt;

Output

&lt;?xml version=&quot;1.0&quot;?&gt;

2

&lt;w:c xmlns:w=&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot;/&gt;

Expected

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

Have try usingxsl:value-of select= to refer value 2.

format.xsl

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

Output only display 2

&lt;?xml version=&quot;1.0&quot;?&gt;

2

答案1

得分: 0

I have added the following XML code:

<?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>

This can generate the desired output:

<?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>
英文:

I have add &lt;xsl:template match=&quot;/&quot;&gt; and using xsl:value-of select=&quot;w:document/w:t&quot;

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

this can generate desire output:

    &lt;?xml version=&quot;1.0&quot;?&gt;
&lt;w:document xmlns:ve=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; xmlns:o=&quot;urn:schemas-microsoft-com:office:office&quot; xmlns:r=&quot;http://schemas.openxmlformats.org/officeDocument/2006/relationships&quot; xmlns:m=&quot;http://schemas.openxmlformats.org/officeDocument/2006/math&quot; xmlns:v=&quot;urn:schemas-microsoft-com:vml&quot; xmlns:wp=&quot;http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing&quot; xmlns:w10=&quot;urn:schemas-microsoft-com:office:word&quot; xmlns:w=&quot;http://schemas.openxmlformats.org/wordprocessingml/2006/main&quot; xmlns:wne=&quot;http://schemas.microsoft.com/office/word/2006/wordml&quot;&gt;
  &lt;w:t&gt;2&lt;/w:t&gt;
  &lt;w:c&gt;2&lt;/w:c&gt;
&lt;/w:document&gt;

huangapple
  • 本文由 发表于 2023年4月19日 16:14:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定