简体   繁体   English

仅查找并选择具有给定属性且其值包含唯一数据的元素

[英]Find and select only element that has a given attribute whose value contains a unique data

Following is the XMl file以下是 XML 文件

enter code here <items> <item itemcode= "ABC10145" code= "74582 >10</item><item itemcode = "CBD748   Code = "9636">20</item> </items>

I want to write a XSLT code that only prints an element that contains an attribute with value = "ABC10145" and also it's item value = "10" .我想编写一个 XSLT 代码,它只打印一个元素,该元素包含一个value = "ABC10145"的属性,并且它的 item value = "10" The rest of data should be eliminated.其余的数据应该被消除。

I have made couple of changes in the input xml我在输入 xml 中做了一些更改

<?xml version="1.0" encoding="UTF-16"?>
<items> 
<item itemcode= "ABC10145" code= "74582" >10</item>
<item itemcode = "CBD748"   code = "9636">20</item> 
</items>

You can use below template to print only attribute with value = "ABC10145" and also it's item value = "10"您可以使用以下模板仅打印 value = "ABC10145" 的属性,并且它的 item value = "10"

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

 <xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="/items/item[@itemcode!='ABC10145' and .!=10]"/>

</xsl:stylesheet>

The expected result can achieved quite simply by:可以很简单地通过以下方式实现预期的结果:

XSLT 1.0 XSLT 1.0

<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:param name="itemcode" select="'ABC10145'"/>
<xsl:param name="value" select="'10'"/>

<xsl:template match="/items">
    <xsl:copy>
        <xsl:for-each select="item[@itemcode=$itemcode and .=$value]">
            <item itemcode="{@itemcode}">
                <xsl:value-of select="."/>
            </item>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

I am assuming you would want to parametrize the search values.我假设您想要参数化搜索值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM