2

修改现有的 xml 以包含标题部分。我正在尝试将“Header”元素的嵌套子元素“按原样”复制到另一个 xml 文件中,但不能。

带有 Header 元素的 XML:

<?xml version="1.0" encoding="utf-8"?>
<QuestionText
  xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents"
  xmlns:s="clr-namespace:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents"
  xmlns:r="clr-namespace:Telerik.Windows.Documents.Model.Revisions;assembly=Telerik.Windows.Documents"
  xmlns:n="clr-namespace:Telerik.Windows.Documents.Model.Notes;assembly=Telerik.Windows.Documents"
  xmlns:th="clr-namespace:Telerik.Windows.Documents.Model.Themes;assembly=Telerik.Windows.Documents"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  **<Header>
    <t:SubHeader SubHeaderAtt1="valA" SubHeaderAtt2="valB">
      <t:SubSubHeader SubSubHeaderAtt1="valX" SubSubHeaderAtt2="valY">
      </t:SubSubHeader>
    </t:SubHeader>
    <t:SubHeader SubHeaderAtt1="valC" SubHeaderAtt2="valD">
      <t:SubSubHeader SubSubHeaderAtt1="valM" SubSubHeaderAtt2="valN">
      </t:SubSubHeader>
    </t:SubHeader>
  </Header>**
  <Content>
    <p ListId="2" xmlns="">
      <span StyleName="ss">Item 1</span>
    </p>
    <p ListId="2" xmlns="">
      <span>Item 2</span>
    </p>
    <p ListId="2" xmlns="">
      <span>Item 3</span>
    </p>
    <p ListId="3" xmlns="" />
  </Content>
</QuestionText>

XSD 复制标头:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
    xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents"
    xmlns:s="clr-namespace:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents"
    xmlns:r="clr-namespace:Telerik.Windows.Documents.Model.Revisions;assembly=Telerik.Windows.Documents"
    xmlns:n="clr-namespace:Telerik.Windows.Documents.Model.Notes;assembly=Telerik.Windows.Documents"
    xmlns:th="clr-namespace:Telerik.Windows.Documents.Model.Themes;assembly=Telerik.Windows.Documents">
    <xsl:output method="xml" indent="yes"/>
  <xsl:template match="QuestionText">
    <t:RadDocument>
      <xsl:copy>
        <xsl:value-of select="Header/node()"/>
      </xsl:copy>
      <t:Section>
        <!-- Avoided for brevity  -->
      </t:Section>
    </t:RadDocument>
  </xsl:template>
</xsl:stylesheet>

预期输出:

    <?xml version="1.0" encoding="utf-8"?>
<t:RadDocument   xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents"
  xmlns:s="clr-namespace:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents"
  xmlns:r="clr-namespace:Telerik.Windows.Documents.Model.Revisions;assembly=Telerik.Windows.Documents"
  xmlns:n="clr-namespace:Telerik.Windows.Documents.Model.Notes;assembly=Telerik.Windows.Documents"
  xmlns:th="clr-namespace:Telerik.Windows.Documents.Model.Themes;assembly=Telerik.Windows.Documents"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <t:SubHeader SubHeaderAtt1="valA" SubHeaderAtt2="valB">
    <t:SubSubHeader SubSubHeaderAtt1="valX" SubSubHeaderAtt2="valY">
    </t:SubSubHeader>
  </t:SubHeader>
  <t:SubHeader SubHeaderAtt1="valC" SubHeaderAtt2="valD">
    <t:SubSubHeader SubSubHeaderAtt1="valM" SubSubHeaderAtt2="valN">
    </t:SubSubHeader>
  </t:SubHeader>
  <t:Section>
    <!-- Avoided for brevity  -->
  </t:Section>
</t:RadDocument>

如果有人能解释我缺少什么或如何解决这个问题,我将不胜感激。任何帮助都可以。

4

1 回答 1

1

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"输入文档的根元素上的 表示andQuestionText元素Header在此命名空间中。所以

<xsl:template match="QuestionText">

不会匹配任何东西,Header/node()也不会选择任何东西,因为 XPath 1.0 中无前缀的元素名称总是意味着“没有命名空间”。

您需要将前缀映射到此名称空间并在 XPath 表达式中使用它。您可能还想使用copy-of而不是value-of复制标题元素

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="xamlp msxsl"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:xamlp="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
    xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents"
    xmlns:s="clr-namespace:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents"
    xmlns:r="clr-namespace:Telerik.Windows.Documents.Model.Revisions;assembly=Telerik.Windows.Documents"
    xmlns:n="clr-namespace:Telerik.Windows.Documents.Model.Notes;assembly=Telerik.Windows.Documents"
    xmlns:th="clr-namespace:Telerik.Windows.Documents.Model.Themes;assembly=Telerik.Windows.Documents">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="xamlp:QuestionText">
    <t:RadDocument>
      <xsl:copy-of select="xamlp:Header/node()"/>
      <t:Section>
        <!-- Avoided for brevity  -->
      </t:Section>
    </t:RadDocument>
  </xsl:template>
</xsl:stylesheet>
于 2013-11-14T12:42:46.780 回答