简体   繁体   English

使用XSLT从XML检索所有属性值

[英]Retrieve all the attribute values from XML using XSLT

I can't figure out how to access all the attributes in a tag from an XML document. 我无法弄清楚如何从XML文档访问标记中的所有属性。

Let's say I have the following XML: 假设我有以下XML:

<names>
  <name firstname="Rocky" lastname="Balboa" divider=", "/>
  <name firstname="Ivan" lastname="Drago" divider=", "/>
</names>

I want the following output: Rocky Balboa, Ivan Drago, 我想要以下输出: Rocky Balboa, Ivan Drago,

What I currently have is: 我目前拥有的是:

<xsl:for-each select="names/name">
   <xsl:value-of select="@firstname"/>
   <xsl:value-of select="@lastname"/>
   <xsl:value-of select="@divider"/>
</xsl:for-each>

What I'm wondering is if it's possible to do this in just one value-of select instead of having to do three of them. 我想知道的是,是否有可能仅通过一个选择值来执行此操作,而不必执行三个操作。 So to clarify, I want to be able to output all the attributes in the tag with one single value-of select. 因此,为了澄清起见,我希望能够使用一个单一的select值输出标记中的所有属性。 Is this possible? 这可能吗?

Thanks. 谢谢。

Because I'm not sure if the use of xsl:value-of is a hard requirement, perhaps something like the following could be what you are locking for. 因为我不确定是否必须使用xsl:value-of ,所以可能锁定了以下内容。

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

    <xsl:template match="name" mode ="print" >
        <xsl:value-of select="@firstname"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="@lastname"/>
        <xsl:value-of select="@divider"/>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates  select="names/name" mode="print"/>
    </xsl:template>

</xsl:stylesheet>

You can use <xsl:apply-templates select="names/name" mode="print"/> at any position you have considered about using a one line value-of for all attributes. 您可以在考虑为所有属性使用单行值-of的任何位置使用<xsl:apply-templates select="names/name" mode="print"/>
The above template will generate the following output: 上面的模板将生成以下输出:

Rocky Balboa, Ivan Drago,

Update crate output without using the attribute names: 在不使用属性名称的情况下更新板条箱输出:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:template match="name" mode ="print" >
        <xsl:for-each select="@*" >
            <xsl:if test="not(position() = last() or position() = 1)">
                <xsl:text> </xsl:text>
            </xsl:if>
            <xsl:value-of select="."/>

        </xsl:for-each>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates  select="names/name" mode="print"/>
    </xsl:template>

</xsl:stylesheet>

try the following: 尝试以下方法:

<xsl:template match="/">
 <xsl:for-each select="names/name/@*">
        <xsl:value-of select="concat( ., ' ')"/>
  </xsl:for-each>
</xsl:template>     

You can use this XPath @* to get all attributes, eg: 您可以使用此XPath @ *获取所有属性,例如:

<xsl:template match="/*">
    <xsl:for-each select="@*">
        <xsl:value-of select="concat(name(), ': ', ., ' ')"/>
    </xsl:for-each>
</xsl:template>

This will let you use just one value-of select to get the output you want. 这将使您仅使用一个选择值即可获得所需的输出。 It will take all attribute into consideration. 它将考虑所有属性。

This should be a sufficient hint for you to figure out things. 这应该足以为您找出问题的线索。 Let me know if you have any other question. 如果您还有其他问题,请告诉我。

If you can use XSLT 2.0, you can do something like this: 如果可以使用XSLT 2.0,则可以执行以下操作:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="text()"/>

    <xsl:template match="*[@*]">
        <xsl:value-of select="@*[not(name()='divider')]" separator=" "/>
        <xsl:value-of select="@divider"/>           
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

This will output all attributes and you have no control over the order, so if you want to specify an order, you can either use a sequence: 这将输出所有属性,并且您无法控制订单,因此,如果要指定订单,则可以使用序列:

<xsl:value-of select="(@firstname,@lastname)" separator=" "/>

or do an xsl:apply-templates with an xsl:sort to sort the attributes by name() (or whatever). 或使用带有xsl:sortxsl:apply-templates来按name() (或任何其他name()对属性进行排序。 Let me know if you'd like an example. 让我知道您是否想举个例子。

The following works in XSLT 2.0: XSLT 2.0中的以下作品:

<xsl:for-each select="names/name">
   <xsl:value-of select="@firstname, @lastname, @divider"/>
</xsl:for-each>

and in 3.0 you can do: 在3.0中,您可以执行以下操作:

<xsl:value-of select="names/name!(@firstname, @lastname, @divider)"/>

though you may need to make adjustments to get the whitespace the way you want it. 尽管您可能需要进行调整才能以所需的方式获得空白。

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

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