XHtml转换 - 复制标题元素的嵌套子元素

时间:2013-11-14 12:27:21

标签: xml xslt

修改现有的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>

我很感激,如果有人可以解释我的遗失或如何解决问题。任何帮助都可以。

1 个答案:

答案 0 :(得分:1)

输入文档的根元素上的xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"表示QuestionTextHeader元素位于此命名空间中。因此

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