Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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_Transformation - Fatal编程技术网

C# 删除元素的xslt转换

C# 删除元素的xslt转换,c#,xml,xslt,transformation,C#,Xml,Xslt,Transformation,我想删除下面xml中包含“tig:”的所有条目。我尝试了下面的转换xsl来转换它,但它不起作用 <ContactInfo> <PersonName> <FormattedName>My Name</FormattedName> <GivenName>Test first Name</GivenName> <FamilyName>Test Last Name

我想删除下面xml中包含“tig:”的所有条目。我尝试了下面的转换xsl来转换它,但它不起作用

 <ContactInfo>
    <PersonName>
        <FormattedName>My Name</FormattedName>
        <GivenName>Test first Name</GivenName>
        <FamilyName>Test Last Name</FamilyName>
    </PersonName>
       </ContactInfo>
    <tig:TestArea>
        <tig:UserArea>
            <tig:ParseTime>9000</tig:ParseTime>
        </tig:UserArea>
        <tig:Country>
            <tig:Language>en</tig:Language>
        <tig:Country>CAN</tig:Country>
        </tig:Country>
    </tig : TestArea>

我的名字
测试名
测试姓氏
9000
EN
可以
我尝试了下面的转换xsl来转换它,但它不起作用。tig:也是一个名称空间,我想删除与该名称空间相关的所有元素

    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>

        <!-- identity template -->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
        </xsl:template>

        <xsl:template match="tig:">
            <xsl:apply-templates/>
        </xsl:template>

    </xsl:stylesheet>

而不是

 <xsl:template match="tig:">
        <xsl:apply-templates/>
    </xsl:template>

使用


首先要说的是,您的XML缺少“tig”前缀的名称空间声明。它还缺少一个根元素,因此我假设在声明名称空间的实际XML中有一个根元素

<Data xmlns:tig="http://tig">
   <ContactInfo>

但是,如果在
tig
元素下嵌套了属于不同命名空间(或根本不在命名空间中)的元素,这些元素也将被删除。

给定格式良好的输入,例如:

<root>
    <ContactInfo>
        <PersonName>
            <FormattedName>My Name</FormattedName>
            <GivenName>Test first Name</GivenName>
            <FamilyName>Test Last Name</FamilyName>
        </PersonName>
    </ContactInfo>
    <tig:TestArea xmlns:tig="http://www.example.com/tig">
            <tig:UserArea>
                <tig:ParseTime>9000</tig:ParseTime>
            </tig:UserArea>
            <tig:Language>en</tig:Language>
            <tig:Country>CAN</tig:Country>
    </tig:TestArea>
</root>
这无法工作,因为(a)
“tig:
不是有效的匹配模式,(2)前缀
tig
未绑定到命名空间。
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:tig="http://tig">
<xsl:template match="tig:*">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="tig:*/text()" />
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:tig="http://tig">

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

    <!-- identity template -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="tig:*">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="tig:*/text()" />
</xsl:stylesheet>
<xsl:template match="tig:*" />
<root>
    <ContactInfo>
        <PersonName>
            <FormattedName>My Name</FormattedName>
            <GivenName>Test first Name</GivenName>
            <FamilyName>Test Last Name</FamilyName>
        </PersonName>
    </ContactInfo>
    <tig:TestArea xmlns:tig="http://www.example.com/tig">
            <tig:UserArea>
                <tig:ParseTime>9000</tig:ParseTime>
            </tig:UserArea>
            <tig:Language>en</tig:Language>
            <tig:Country>CAN</tig:Country>
    </tig:TestArea>
</root>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="tig:*" xmlns:tig="http://www.example.com/tig"/>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <ContactInfo>
      <PersonName>
         <FormattedName>My Name</FormattedName>
         <GivenName>Test first Name</GivenName>
         <FamilyName>Test Last Name</FamilyName>
      </PersonName>
   </ContactInfo>
</root>