开发者

access the value of attribute (as a variable)

开发者 https://www.devze.com 2023-01-14 12:23 出处:网络
I\'m trying to transform my xml file: <root> <group id=\"F_123\" > <term id=\"F_123_d\" >

I'm trying to transform my xml file:

<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"&g开发者_运维百科t;
                                    <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>

I want to access the value of @group/term/@id and make one element per each term in <group id="A_25">. Is it possible?


It's not very clear what exactly do you want, so a guess follows:

<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>

When applied to your example (I had to add a root element to it, so that it's valid), it produces:

<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>


I want to access the value of @group/term/@id and make one element per each term in <group id="A_25">. Is it possible?

This XPath expression select what I think you want:

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

Also, this stylesheet process what I think you want:

<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>

Output:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号