访问属性的值(作为变量)

时间:2010-08-31 11:47:54

标签: xslt

我正在尝试转换我的xml文件:

<root>
    <group id="F_123" >
        <term id="F_123_d" >
            <word>blabla</word>
            <instruction>blabla</instruction>
        </term>
        <term id="F_123">
            <word>blabla</word>
            <instruction>blabla</instruction>
            <numbers>
                <number code="01" >1</number>
                <number code="02" >2</number>
                <number code="03" >3</number>
                <number code="04" >4</number>
                <number code="05" >5</number>
            </numbers>
        </term>
        <term id="F_124">
            <word>blabla</word>
            <numbers>
                <number code="01" >1</number>
                <number code="02" >2</number>
                <number code="03" >3</number>
                <number code="04" >4</number>
                <number code="05" >5</number>
            </numbers>
        </term>
        <term id="F_125">
            <word>blabla</word>
            <numbers>
                <number code="01" >1</number>
                <number code="02" >2</number>
                <number code="03" >3</number>
                <number code="04" >4</number>
                <number code="05" >5</number>
            </numbers>
        </term>
        <routing id="F_123_1">
            <condition>
                <operator type="or">
                    <operator type="or">
                        <operator type="equal">
                            <variable name="F_D01a3DE1"/>
                            <constant>DK</constant>
                        </operator>
                        <operator type="equal">
                            <variable name="F_D01a3DE1"/>
                            <constant>RF</constant>
                        </operator>
                    </operator>
                    <operator type="equal">
                        <variable name="F_D01a3DE1"/>
                        <constant>1</constant>
                    </operator>
                </operator>
            </condition>
            <then>
                <goto group="A_24"/>
            </then>
            <else>
                <routing>
                    <condition>
                        <operator type="or">
                            <operator type="or">
                                <operator type="equal">
                                    <variable name="B_D01a3DE1"/>
                                    <constant>5</constant>
                                </operator>
                                <operator type="equal">
                                    <variable name="B_D01a3DE1"/>
                                    <constant>10</constant>
                                </operator>
                            </operator>
                            <operator type="equal">
                                <variable name="B_D01a3DE1"/>
                                <constant>7</constant>
                            </operator>
                        </operator>
                    </condition>
                    <then>
                        <goto group="A_25"/>
                    </then>
                    <else>
                        <routing>
                            <condition>
                                <operator type="or">
                                    <operator type="equal">
                                        <variable name="B_D01a3DE1"/>
                                        <constant>6</constant>
                                    </operator>
                                    <operator type="equal">
                                        <variable name="B_D01a3DE1"/>
                                        <constant>11</constant>
                                    </operator>
                                </operator>
                            </condition>
                            <then>
                                <goto group="A_26"/>
                            </then>
                            <else>
                                <goto group="A_27"/>
                            </else>
                        </routing>
                    </else>
                </routing>
            </else>
        </routing>
    </group>
    <group id="A_25" >
        <term id="A_25" >
            <word>blabla</word>
            <instruction>blabla</instruction>
        </term>
        <term id="A_26">
            <word>blabla</word>
            <instruction>blabla</instruction>
            <numbers>
                <number code="01" >1</number>
                <number code="02" >2</number>
            </numbers>
        </term>
    </group>
</root>

我想访问@ group / term / @ id的值,并在<group id="A_25">中为每个术语创建一个元素。有可能吗?

2 个答案:

答案 0 :(得分:2)

目前还不是很清楚你想要什么,所以猜测如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="group/term/@id">
    <id>
      <xsl:value-of select="."/>
    </id>
  </xsl:template>

  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="node()" />
    </root>
  </xsl:template>

  <xsl:template match="node()|@*">
    <xsl:apply-templates select="node()|@*" />
  </xsl:template>
</xsl:stylesheet>

当应用于您的示例时(我必须向其添加根元素,以使其有效),它会生成:

<root>
  <id>F_123_d</id>
  <id>F_123</id>
  <id>F_124</id>
  <id>F_125</id>
  <id>A_25</id>
  <id>A_26</id>
</root>

答案 1 :(得分:1)

  

我想访问的值   @ group / term / @ id并制作一个元素   <group id="A_25">中的每个字词。是   有可能吗?

此XPath表达式选择我想要的内容:

/root/group[@id='A_25']/term/@id 

此外,此样式表处理我认为您想要的内容:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text()"/>
    <xsl:template match="group[@id='A_25']/term">
        <element id="{@id}"/>
    </xsl:template>
</xsl:stylesheet>

输出:

<element id="A_25" />
<element id="A_26" />