如果子节点等于使用 xsl 的 xslt 中的特定值,如何 select 父节点
How to select the parent node if the child node equals specific value in xslt using xsl
我的XML长得像
<Subjects>
<subject>
<id>Maths</id>
<sid>12</sid>
</subject>
<subject>
<id>science</id>
<sid>13</sid>
</subject>
<subject>
<id>social</id>
<sid>14</sid>
</subject>
</Subjects>
<xsl:variable name="Msubject" select="../../id[text()='Maths']"/>
<xsl:value-of select="$Msubject/sid/text()" />
<xsl:template match="//subject[id='Maths']/sid">
<xsl:copy>
22
</xsl:copy>
</xsl:template>
我需要获取 id = Maths 的主题并将其 sid 更改为 22(或要求的值)。类似地,我需要获取 id 为 science 的主题,并将其 sid 更改为 23 等等,上面尝试过但不起作用**
根据问题中提供的额外输入,解决方案已修改,现在可用于将 <sid>
中的值替换为不同文件中可用的值。
File1.xml
<Subjects>
<subject>
<id>Maths</id>
<sid>12</sid>
</subject>
<subject>
<id>science</id>
<sid>13</sid>
</subject>
<subject>
<id>social</id>
<sid>14</sid>
</subject>
</Subjects>
File2.xml
<inputfile>
<mathsid>22</mathsid>
<scienceid>23</scienceid>
<socialid>24</socialid>
</inputfile>
XSLT 使用逻辑从 File1.xml
读取 <id>
的值并从中创建一个节点,即。 <Mathsid>
、<scienceid>
和 <socialid>
。使用这个制造的节点,它在 File2.xml
中查找包含相似名称的节点。匹配从第二个字符开始,因此 <Mathsid>
中的大写字母 M 不会导致 <mathsid>
出现问题。也可以通过将驼峰式节点名称转换为小写来实现,但 XSLT 1.0 需要额外的工作来处理它。
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:param name="file" select="document('File2.xml')/inputfile" />
<!-- identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- for all 'subject' nodes -->
<xsl:template match="subject">
<!-- create variable to fabricate a node name i.e. Mathsid, scienceid,
socialid using the value in <id> and string 'id' -->
<xsl:variable name="subjNameNode" select="concat(id, 'id')" />
<xsl:copy>
<!-- copy the value of node id as is -->
<xsl:apply-templates select="id" />
<!-- replace existing value of 'sid' by value from the different file by checking
whether the node name contains the explicitly created above. The check is done
from the 2nd character so that <mathsid> contains text 'athsid' -->
<sid>
<xsl:value-of select="$file/*[contains(local-name(), substring($subjNameNode,2))]" />
</sid>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
与 File1.xml
一起使用时,XSLT 会对其进行转换并生成所需的输出。
输出
<Subjects>
<subject>
<id>Maths</id>
<sid>22</sid>
</subject>
<subject>
<id>science</id>
<sid>23</sid>
</subject>
<subject>
<id>social</id>
<sid>24</sid>
</subject>
</Subjects>
我的XML长得像
<Subjects>
<subject>
<id>Maths</id>
<sid>12</sid>
</subject>
<subject>
<id>science</id>
<sid>13</sid>
</subject>
<subject>
<id>social</id>
<sid>14</sid>
</subject>
</Subjects>
<xsl:variable name="Msubject" select="../../id[text()='Maths']"/>
<xsl:value-of select="$Msubject/sid/text()" />
<xsl:template match="//subject[id='Maths']/sid">
<xsl:copy>
22
</xsl:copy>
</xsl:template>
我需要获取 id = Maths 的主题并将其 sid 更改为 22(或要求的值)。类似地,我需要获取 id 为 science 的主题,并将其 sid 更改为 23 等等,上面尝试过但不起作用**
根据问题中提供的额外输入,解决方案已修改,现在可用于将 <sid>
中的值替换为不同文件中可用的值。
File1.xml
<Subjects>
<subject>
<id>Maths</id>
<sid>12</sid>
</subject>
<subject>
<id>science</id>
<sid>13</sid>
</subject>
<subject>
<id>social</id>
<sid>14</sid>
</subject>
</Subjects>
File2.xml
<inputfile>
<mathsid>22</mathsid>
<scienceid>23</scienceid>
<socialid>24</socialid>
</inputfile>
XSLT 使用逻辑从 File1.xml
读取 <id>
的值并从中创建一个节点,即。 <Mathsid>
、<scienceid>
和 <socialid>
。使用这个制造的节点,它在 File2.xml
中查找包含相似名称的节点。匹配从第二个字符开始,因此 <Mathsid>
中的大写字母 M 不会导致 <mathsid>
出现问题。也可以通过将驼峰式节点名称转换为小写来实现,但 XSLT 1.0 需要额外的工作来处理它。
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<xsl:param name="file" select="document('File2.xml')/inputfile" />
<!-- identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- for all 'subject' nodes -->
<xsl:template match="subject">
<!-- create variable to fabricate a node name i.e. Mathsid, scienceid,
socialid using the value in <id> and string 'id' -->
<xsl:variable name="subjNameNode" select="concat(id, 'id')" />
<xsl:copy>
<!-- copy the value of node id as is -->
<xsl:apply-templates select="id" />
<!-- replace existing value of 'sid' by value from the different file by checking
whether the node name contains the explicitly created above. The check is done
from the 2nd character so that <mathsid> contains text 'athsid' -->
<sid>
<xsl:value-of select="$file/*[contains(local-name(), substring($subjNameNode,2))]" />
</sid>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
与 File1.xml
一起使用时,XSLT 会对其进行转换并生成所需的输出。
输出
<Subjects>
<subject>
<id>Maths</id>
<sid>22</sid>
</subject>
<subject>
<id>science</id>
<sid>23</sid>
</subject>
<subject>
<id>social</id>
<sid>24</sid>
</subject>
</Subjects>