1

次の XML ドキュメントがあるとします。

<Element0 AttributeA="A">
  <Element1 AttributeB="1" AttributeC="C" AttributeD="D">
    <Element2>nodeValue</Element2>
  </Element1>
  <Element1 AttributeB="2" AttributeC="C" AttributeD="D">
    <Element2>nodeValue</Element2>
    <Element3 AttributeE="E">
        <Element4 AttributeF="F">nodeValue</Element4>
    </Element3>
  </Element1>
  .
  .
  .
  .
</Element0>

xml ドキュメントの内容を事前に知らに、ドキュメントを個々の xpath (以下を参照) に解析 (シュレッド、分解、変換) するにはどうすればよいですか?

//Element0[@AttributeA='A']/Element1[@AttributeB='1' and @AttributeC='C' and @AttributeD='D']/Element2
//Element0[@AttributeA='A']/Element1[@AttributeB='2' and @AttributeC='C' and @AttributeD='D']/Element2
//Element0[@AttributeA='A']/Element1[@AttributeB='2' and @AttributeC='C' and @AttributeD='D']/Element3[@AttributeE='E']/Element4[@AttributeF='F']
4

2 に答える 2

1

これは私がそれを行う方法です。兄弟の 1 つとまったく同じ属性を持っている場合でも、要素ごとに完全に一意の XPath を取得できるように、位置も含めていることに注意してください。

XML 入力

<Element0 AttributeA="A">
    <Element1 AttributeB="1" AttributeC="C" AttributeD="D">
        <Element2>nodeValue</Element2>
    </Element1>
    <Element1 AttributeB="2" AttributeC="C" AttributeD="D">
        <Element2>nodeValue</Element2>
        <Element3 AttributeE="E">
            <Element4 AttributeF="F">nodeValue</Element4>
        </Element3>
    </Element1>
</Element0>

XSLT1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="*">
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="concat('/',local-name())"/>
            <xsl:value-of select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/>
            <xsl:if test="@*">
                <xsl:text>[</xsl:text>
                <xsl:apply-templates select="@*"/>
                <xsl:text>]</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
        <xsl:apply-templates select="node()"/>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:if test="position() != 1">
            <xsl:text> and </xsl:text>
        </xsl:if>
        <xsl:value-of select="concat('@',local-name(),'=&quot;',.,'&quot;')"/>
    </xsl:template>

</xsl:stylesheet>

出力

/Element0[1][@AttributeA="A"]
/Element0[1][@AttributeA="A"]/Element1[1][@AttributeB="1" and @AttributeC="C" and @AttributeD="D"]
/Element0[1][@AttributeA="A"]/Element1[1][@AttributeB="1" and @AttributeC="C" and @AttributeD="D"]/Element2[1]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element2[1]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element3[1][@AttributeE="E"]
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element3[1][@AttributeE="E"]/Element4[1][@AttributeF="F"]
于 2013-03-05T03:28:52.307 に答える
0

XMLStarletを として使用すると、XMLStarlet は非常によく似た動作をしますxml el -v file.xml。値を持つパスと属性が表示されます。ただし、そのノードが先端である場合、パスの途中で属性を与えることはありません。あなたの例では、次のものが生成されます。

Element0[@AttributeA='A']
Element0/Element1[@AttributeB='1' and @AttributeC='C' and @AttributeD='D']
Element0/Element1/Element2
Element0/Element1[@AttributeB='2' and @AttributeC='C' and @AttributeD='D']
Element0/Element1/Element2
Element0/Element1/Element3[@AttributeE='E']
Element0/Element1/Element3/Element4[@AttributeF='F']
于 2013-03-05T03:27:25.837 に答える