Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XSLT转换:如何保留元素';在转换过程中,属性是什么?_C#_Xml_Xslt - Fatal编程技术网

C# XSLT转换:如何保留元素';在转换过程中,属性是什么?

C# XSLT转换:如何保留元素';在转换过程中,属性是什么?,c#,xml,xslt,C#,Xml,Xslt,我在C#中有一个函数,用于获取XML数据并使用XSLT样式表对其进行排序,然后返回排序后的数据并将其放入XMLDocument对象中。XSLT将无误地处理数据,但不会正确返回所有数据。正如您在下面看到的,CarerContent的属性缺失 这是XSLT: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="Match

我在C#中有一个函数,用于获取XML数据并使用XSLT样式表对其进行排序,然后返回排序后的数据并将其放入XMLDocument对象中。XSLT将无误地处理数据,但不会正确返回所有数据。正如您在下面看到的,CarerContent的属性缺失

这是XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
  <xsl:template match="MatchedSources">
    <xsl:copy>
      <xsl:apply-templates>
         <xsl:sort data-type="number" order="descending" select="OverallMatchValue"/>
         </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

输入XML数据如下所示:

<?xml version="1.0"?>
<MatchedSources responseId="1" dataSourceType="Document">
    <MatchedSource>
        <SourceId>1001</SourceId>
        <DifferentPerspectives>
            <Carer>
                <CarerContent id="1" title="text">content</CarerContent>
                <CarerContent id="2" title="text">content</CarerContent>
            </Carer>
        </DifferentPerspectives>
        <OverallMatchValue>45</OverallMatchValue>
    </MatchedSource>
   <MatchedSource>
        <SourceId>1002</SourceId>
        <DifferentPerspectives>
            <Carer>
                <CarerContent id="1" title="text">content</CarerContent>
                <CarerContent id="2" title="text">content</CarerContent>
            </Carer>
        </DifferentPerspectives>
        <OverallMatchValue>78</OverallMatchValue>
    </MatchedSource>
</MatchedSources>

1001
所容纳之物
所容纳之物
45
1002
所容纳之物
所容纳之物
78
以及生成的输出XML:

<?xml version="1.0"?>
<MatchedSources responseId="1" dataSourceType="Document">
    <MatchedSource>
        <SourceId>1002</SourceId>
        <DifferentPerspectives>
            <Carer>
                <CarerContent id="1" title="text">content</CarerContent>
                <CarerContent id="2" title="text">content</CarerContent>
            </Carer>
        </DifferentPerspectives>
        <OverallMatchValue>78</OverallMatchValue>
    </MatchedSource>
   <MatchedSource>
        <SourceId>1001</SourceId>
        <DifferentPerspectives>
            <Carer>
                <CarerContent>content</CarerContent>
                <CarerContent>content</CarerContent>
            </Carer>
        </DifferentPerspectives>
        <OverallMatchValue>45</OverallMatchValue>
    </MatchedSource>
</MatchedSources>

1002
所容纳之物
所容纳之物
78
1001
所容纳之物
所容纳之物
45

是的,这是一个常见的容易犯的错误。
只复制元素本身,它相当于
。您需要明确地复制属性节点

例如,只需通过以下方式切换默认模板:

<xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

或者,如果您希望某些属性具有特定行为,请使用完整的“匹配设计”:


是的,这是一个常见的容易犯的错误。
只复制元素本身,它相当于
。您需要明确地复制属性节点

例如,只需通过以下方式切换默认模板:

<xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

或者,如果您希望某些属性具有特定行为,请使用完整的“匹配设计”:


您应该了解-并使用它而不是第二个模板。

您应该了解-并使用它而不是第二个模板