简体   繁体   English

如何使用XSLT从具有编号出现次数的非结构化XML中选择值

[英]How to select values with XSLT from unstructed XML with numbered occurences

For a label printing function I'm trying to understand how I can use XSLT and value-of select to retrieve values from an unstructed xml-output. 对于标签打印功能,我试图了解如何使用XSLT和value-of select从非结构化xml输出中检索值。

See the example below where the tags I3 and I4 is related if the "occ" (occurence) number is equal between the two tags. 如果两个标签之间的“ occ”(出现)数相等,请参见下面的示例,其中标签I3和I4相关。

The I4 tag describes the type of value, and the I3 is the actual value. I4标签描述值的类型,而I3是实际值。 For instance: "I3" occ="1">123 is related to "I4" occ="1" other number. 例如:“ I3” occ =“ 1”> 123与“ I4” occ =“ 1”其他数字有关。

example: 例:

<field tag="I3" occ="1">123</field>
<field tag="I4" occ="1" lang="sv-SE" invariant="true">other number</field>
<field tag="I3" occ="2">324</field>
<field tag="I4" occ="2" lang="sv-SE" invariant="true">find number</field>
<field tag="I3" occ="3">1203</field>
<field tag="I4" occ="3" lang="sv-SE" invariant="true">FID</field>
<field tag="I3" occ="4">321-35-2000</field>
<field tag="I4" occ="4" lang="sv-SE" invariant="true">archive number</field>

Is there a way with XSLT 1.0 to do this kind of select operation? XSLT 1.0可以执行这种选择操作吗? My knowledge is limited to how I could select for instance the tag I3 with occ=3, as such: 我的知识仅限于如何选择带有occ = 3的标记I3,例如:

<xsl:value-of select="field[@tag='I3' and @occ='4']" /></xsl:text>

but the data I'm selecting values from will not be similiar each time (the occurance och the type-tag will not always have the same order, and sometimes on or several of the types will not exist). 但是我每次从中选择值的数据都不会相似(类型标记的出现并不总是具有相同的顺序,有时不存在一种或几种类型)。

Any suggestions? 有什么建议么? Thanks for any input! 感谢您的输入!

The output I want is something like: 我想要的输出是这样的:

<data>other number: 123</data>
<data>find number: 324</data>
<data>FID: 1203</data>
<data>archive number: 321-35-2000</data> 

Updated output - I realised that I need each I4 tag as element name, otherwise the printer service cannot use the lines as source for printing output. 更新的输出-我意识到我需要每个I4标签作为元素名称,否则打印机服务不能将这些行用作打印输出的源。

<?xml version="1.0" encoding="UTF-8"?>  
<output>  
  <variable name="other number">123</variable>  
  <variable name="find number">324</variable>  
  <variable name="FID">1203</variable>  
  <variable name="archive number">321-35-2000</variable>  
</output>

If you can be sure that one of the elements of the pair will always be there, you could use that element for your initial selection. 如果可以确定该对中的一个元素始终存在,则可以将该元素用于初始选择。 Then use a key to retrieve the other member of the pair (which may or may not exist). 然后使用密钥检索该对中的另一个成员(可能存在或可能不存在)。

In this example we assume that <field tag="I3"> always exists: 在此示例中,我们假定<field tag="I3">始终存在:

XML XML

<fields>
    <field tag="I3" occ="1">123</field>
    <field tag="I4" occ="1" lang="sv-SE" invariant="true">other number</field>
    <field tag="I3" occ="2">324</field>
    <field tag="I4" occ="2" lang="sv-SE" invariant="true">find number</field>
    <field tag="I3" occ="3">1203</field>
    <field tag="I4" occ="3" lang="sv-SE" invariant="true">FID</field>
    <field tag="I3" occ="4">321-35-2000</field>
    <field tag="I4" occ="4" lang="sv-SE" invariant="true">archive number</field>
</fields>

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:strip-space elements="*"/>

<xsl:key name="type" match="field[@tag='I4']" use="@occ" />

<xsl:template match="/fields">
    <output>
        <xsl:for-each select="field[@tag='I3']">
            <data>
                <xsl:value-of select="key('type', @occ)"/>
                <xsl:text>: </xsl:text>
                <xsl:value-of select="."/>
            </data>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

Result 结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <data>other number: 123</data>
  <data>find number: 324</data>
  <data>FID: 1203</data>
  <data>archive number: 321-35-2000</data>
</output>

Added: 添加:

If you change: 如果您更改:

            <data>
                <xsl:value-of select="key('type', @occ)"/>
                <xsl:text>: </xsl:text>
                <xsl:value-of select="."/>
            </data>

to: 至:

            <variable name="{key('type', @occ)}">
                <xsl:value-of select="."/>
            </variable>

the result will be: 结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <variable name="other number">123</variable>
  <variable name="find number">324</variable>
  <variable name="FID">1203</variable>
  <variable name="archive number">321-35-2000</variable>
</output>

Read about attribute value templates . 阅读有关属性值模板的信息

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

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