入力XMLが任意であるXSLT変換を実行しようとしています。その中で一定している唯一のことは、名前が「first」で始まるノードを持つことです。そのノードの値を取得する必要があり、それは直接の兄弟です。ただし、次のテンプレートはXML宣言のみを生成します。
重要な場合、このコードはNokogiriXMLパーサーを使用するRubyにあります。ただし、これはRubyの質問ではなく、XSLT / XPathの質問であるため、それに応じてタグ付けされていると思います。
入力XML:
<?xml version="1.0"?>
<employees>
<employee>
<first_name>Winnie</first_name>
<last_name>the Pooh</last_name>
</employee>
<employee>
<first_name>Jamie</first_name>
<last_name>the Weeh</last_name>
</employee>
</employees>
必要な出力XML:
<?xml version="1.0"?>
<people>
<person>
<first>Winnie</first>
<last>the Pooh</last>
</person>
<person>
<first>Jamie</first>
<last>the Weeh</last>
</person>
</people>
XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="/">
<xsl:for-each select="node()[starts-with(name(), 'first')]">
<people>
<person>
<name>
<first><xsl:value-of select="." /></first>
<last><xsl:value-of select="following-sibling::*[1]" /></last>
</name>
</person>
</people>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>