简体   繁体   English

如何使用 XSLT 从 XML 获取属性?

[英]How to get the attributes from an XML using XSLT?

This is the XML I am using from which I have to get the attributes values fort all the property name.这是我正在使用的 XML,我必须从中获取所有属性名称的属性值。

<?xml version="1.0" encoding="UTF-8"?>
<prodxml>
<prodxml><revisionNumber>05</revisionNumber><RosettaNet>
<Property name="Brush Thickness" ShortName="BRTHK" LongDesc="Brush Thickness" Code="1750" SubCode="" UCL="0.4400" LCL="0.4120"/>
<Property name="Bristle Diameter" ShortName="BAAA" LongDesc="Bristle Diameter" Code="306" SubCode="" UCL="0.005317" LCL="0.004806"/>
<Property name="Bristle Density" ShortName="BRD" LongDesc="Bristle Density" Code="305" SubCode="" UCL="96.2" LCL="72.0"/>
<Property name="Resin Density" ShortName="RDEN" LongDesc="Resin   Density" Code="1749" SubCode="" UCL="203" LCL="193"/>
<Property name="Brush Thickness Range" ShortName="BRTHR" LongDesc="Brush Thickness   Range" Code="2442" SubCode="" UCL="0.012" LCL="0"/>
<Property name="Bristle Diameter Range" ShortName="BAAD" LongDesc="Bristle Diameter   Range" Code="311" SubCode="" UCL="0.00105" LCL="0"/>
<Property name="Bristle Density Range" ShortName="BRDR" LongDesc="Bristle Density   Range" Code="310" SubCode="" UCL="29" LCL="0"/>
<Property name="Resin Density Range" ShortName="RDENR" LongDesc="Resin Density Range" Code="1753" SubCode="" UCL="20" LCL="0"/>
</RosettaNet>
<B2B_Customer>
<RecipientName>Intel</RecipientName>
</B2B_Customer>
</prodxml>
</prodxml>

This is the XSLT I am using, but unable to retrieve values.这是我正在使用的 XSLT,但无法检索值。 can anyone please help?有人可以帮忙吗?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <xsl:element name="UCL" namespace="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">
         <xsl:value-of select="//RosettaNet/Property/@UCL" />
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

Thanks for the reply.谢谢回复。 Please find the expected output请找到预期的输出

<?xml version="1.0"?>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.4400</UCL>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.005317</UCL>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">96.2</UCL>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">203</UCL>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.012</UCL>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.00105</UCL>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">29</UCL>
<UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">20</UCL>

You're not currently doing any iteration over the various nodes.您当前没有对各个节点进行任何迭代。

In XSLT this is done in two ways:在 XSLT 中,这是通过两种方式完成的:

  • <xsl:for-each> - a traditional loop common in most languages <xsl:for-each> - 大多数语言中常见的传统循环

  • <xsl:apply-templates> - ie passing each node iteratively to a template for further processing <xsl:apply-templates> - 即迭代地将每个节点传递给模板以供进一步处理

Here's an example with the latter:这是后者的示例:

<!-- root template -->
<xsl:template match="/">
    <xsl:apply-templates match='//RosettaNet/Property' />
</xsl:template>

<!-- match 'Property' nodes and output 'UCL' node for each one -->
<xsl:template match='Property'>
    <xsl:element name="UCL" namespace="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">
        <xsl:value-of select="@UCL" />
    </xsl:element>          
</xsl:template>

Within XSLT there are two basic paradigms for nodal access (which includes, among the other nodal types, the attributes in your question).在 XSLT 中,节点访问有两种基本范式(其中包括您问题中的属性等其他节点类型)。 These paradigms are referred to as " push " and " pull " for short.这些范式被简称为“”和“”。

" Push " involves using template matching to access the content in a more flow-like sort of manner. 推送”涉及使用模板匹配以更类似流程的方式访问内容。 In other words, when the XSLT processor (eg Saxon) pushes a node through which is a match for a template (or templates), the template(s) is/are activated.换句话说,当XSLT 处理器(例如Saxon)推送一个节点,通过该节点与一个(或多个)模板匹配时,该(一个或多个)模板被激活。 (Let's stick with 'template' in the singular for now. If you have more than one template match, that gets into something called priority settings, which you should learn about, but which exceeds the scope of this question.) According to the "superman" of XSLT, Dr. Michael Kay, push is an all-too-underused paradigm, and more stylesheets ought to be written to take advantage of this, for many reasons. (现在让我们坚持使用单数形式的“模板”。如果您有多个模板匹配,则会进入称为优先级设置的内容,您应该了解它,但这超出了本问题的范围。)根据“ XSLT 的超人”,Michael Kay 博士,push 是一个完全没有得到充分利用的范例,出于多种原因,应该编写更多的样式表来利用这一点。

" Pull " involves specifically yanking out, if you will, nodes/content that you need.如果您愿意,“”涉及专门拉出您需要的节点/内容。 The processor goes out and gets whatever you are looking for, if available - no templates required.如果可用,处理器就会出去并获取您正在寻找的任何内容 - 无需模板。

With regards to your particular problem, the following stylesheet (which is valid under XSLT 1.0 as well):关于您的特定问题,以下样式表(在 XSLT 1.0 下也有效):

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

    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:call-template name="pull_example"/>
        <!-- <xsl:call-template name="push_example"/> -->
    </xsl:template>

    <xsl:template name="pull_example">
        <!-- By "Pull" -->
        <Root>
            <xsl:for-each select="//Property/@UCL">
                <xsl:element name="UCL" namespace="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:for-each>
        </Root>
    </xsl:template>

    <xsl:template name="push_example">
        <!-- By "Push" -->
        <xsl:element name="Root">
            <xsl:apply-templates select="//Property/@UCL" mode="push_mode"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="//Property/@UCL" mode="push_mode">
        <xsl:element name="UCL" namespace="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Applied to your data :应用于您的数据

<?xml version="1.0" encoding="UTF-8"?>
<prodxml>
    <prodxml>
        <revisionNumber>05</revisionNumber>
        <RosettaNet>
            <Property name="Brush Thickness" ShortName="BRTHK" LongDesc="Brush Thickness" Code="1750" SubCode="" UCL="0.4400" LCL="0.4120"/>
            <Property name="Bristle Diameter" ShortName="BAAA" LongDesc="Bristle Diameter" Code="306" SubCode="" UCL="0.005317" LCL="0.004806"/>
            <Property name="Bristle Density" ShortName="BRD" LongDesc="Bristle Density" Code="305" SubCode="" UCL="96.2" LCL="72.0"/>
            <Property name="Resin Density" ShortName="RDEN" LongDesc="Resin   Density" Code="1749" SubCode="" UCL="203" LCL="193"/>
            <Property name="Brush Thickness Range" ShortName="BRTHR" LongDesc="Brush Thickness   Range" Code="2442" SubCode="" UCL="0.012" LCL="0"/>
            <Property name="Bristle Diameter Range" ShortName="BAAD" LongDesc="Bristle Diameter   Range" Code="311" SubCode="" UCL="0.00105" LCL="0"/>
            <Property name="Bristle Density Range" ShortName="BRDR" LongDesc="Bristle Density   Range" Code="310" SubCode="" UCL="29" LCL="0"/>
            <Property name="Resin Density Range" ShortName="RDENR" LongDesc="Resin Density Range" Code="1753" SubCode="" UCL="20" LCL="0"/>
        </RosettaNet>
        <B2B_Customer>
            <RecipientName>Intel</RecipientName>
        </B2B_Customer>
    </prodxml>
</prodxml>

Yields this output :产生这个输出

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.4400</UCL>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.005317</UCL>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">96.2</UCL>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">203</UCL>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.012</UCL>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">0.00105</UCL>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">29</UCL>
   <UCL xmlns="x-schema:../Schema/ChemGasQualityCertificateSchema2001Jul.xml">20</UCL>
</Root>

(Please note that I wrapped your expected output in a <Root> tag, as otherwise, it would be considered non-well-formed .) (请注意,我将您的预期输出包装在<Root>标签中,否则,它将被视为格式不正确的.)

For all this and more, please see Push, Pull, Next!对于所有这些以及更多内容,请参阅推、拉、下一步! , which describes in detail the aspects/virtues of each methodology in more detail, including Michael Kay's take. ,其中更详细地描述了每种方法的方面/优点,包括 Michael Kay 的看法。

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

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