如何通过值匹配 xml 属性名称并仅从元素列表中显示该元素

问题描述 投票:0回答:1

我是 xslt 的新手。我只想显示 xml 列表中匹配的项目。 使用 xslt 转换。

在这种情况下,我只想显示名称为“一”和“二”的属性

提前致谢。

xml代码:

<myparent>
    <mychild>
        <mygrndchild name="a" parent="1" >                       
            <attribute name="one" value="1"/>
            <attribute name="two" value="1"/>
            <attribute name="three" value="1"/>
            <attribute name="four" value="0"/>            
        </mygrndchild>      
        <mygrndchild name="b" parent="1" >                       
            <attribute name="one2" value="2"/>
            <attribute name="two2" value="3"/>
            <attribute name="three2" value="4"/>
            <attribute name="four2" value="5"/>            
        </mygrndchild>
    </mychild>
</myparent>

xslt 代码:


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <!--suppresses attribute in list-->
    
<xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node() "/>
        </xsl:copy>
    </xsl:template>
    
     <xsl:template match="/parent">
        <xsl:copy>
            <xsl:apply-templates select="child"/>
        </xsl:copy>
     </xsl:template>


     <xsl:template match="/child">
        <xsl:copy>
            <xsl:apply-templates select="grandchild"/>
        </xsl:copy>
     </xsl:template>
     
     <xsl:template match="/grandchild">
        <xsl:copy>
            <xsl:apply-templates select="attribute"/>
        </xsl:copy>
     </xsl:template>

     <xsl:template match="/attribute">
        <xsl:copy>
            <xsl:apply-templates select="@*[. = 'one']"/>
        </xsl:copy>
     </xsl:template>

  <xsl:template match="/attribute">
        <xsl:copy>
            <xsl:apply-templates select="@*[. = 'two']"/>
        </xsl:copy>
     </xsl:template>

</xsl:stylesheet>

此代码按原样输出 xml。

预期结果:

<myparent>
    <mychild>
        <mygrndchild name="a" parent="1" >                       
            <attribute name="one" value="1"/>           
        </mygrndchild>      
        <mygrndchild name="a" parent="1" > 
            <attribute name="two" value="1"/>   
        </mygrndchild>
    </mychild>
</myparent>

我提前感谢您的帮助。 这是为了我学习一些 xml 转换。

xml xslt-2.0
1个回答
0
投票

我怀疑你想要类似的东西

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all">

  <xsl:param name="att-list" as="xs:string*" select="'one', 'two'"/>
  
  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="attribute[not(@name = $att-list)]"/>

</xsl:stylesheet>

如果您确实使用 XSLT 2 处理器,请将

xsl:mode
声明替换为身份转换模板(您发布的代码中的第一个)。

或者根据想要的结果,假设 XSLT 3,您可以使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all">
  
  <xsl:output indent="yes"/>

  <xsl:param name="att-list" as="xs:string*" select="'one', 'two'"/>
  
  <xsl:mode on-no-match="shallow-copy"/>
  
  <xsl:template match="mychild">
    <xsl:copy>
      <xsl:copy-of select="snapshot(.//attribute[@name = $att-list])/ancestor::mygrndchild"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.