xslt - XSLT:如何查看嵌套元素
问题描述
那是我需要应用 xslt 的 xml:
<document>
<component>
<structuredBody>
<component>
<section>
<identifier code="S001"/>
<...>
</section>
</component>
</structuredBody>
</component>
</document>
如您所见,这里有很多我不需要的嵌套结构。
我只需要section
在哪里偷看元素section>identifier.code = "S001"
。
我想在不考虑上部结构的情况下查看我想要的元素。
我正在使用这个 xslt 但它没有偷看我想要的section
元素:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="//section[identifier/@code = 'S001']"/>
</xsl:template>
</xsl:stylesheet>
但我得到:
<?xml version="1.0" encoding="UTF-8"?>
上面的例子是为了简化我的问题而减少的努力:
<document>
<component>
<structuredBody>
<component>
<section>
<identifier code="S001"/>
<table>
<tbody>
<tr>
<td>attribute1</td>
<td>value1</td>
</tr>
<tr>
<td>attribute2</td>
<td>value2</td>
</tr>
<tr>
<td>attribute3</td>
<td>value3</td>
</tr>
</tbody>
</table>
</section>
<section>
<identifier code="S002"/>
<table>
...
</table>
</section>
</component>
</structuredBody>
</component>
</document>
我真正需要的是得到类似的东西:
<person> <!-- -> section-->
<attribute key="attribute1">value1</attribute>
<attribute key="attribute2">value2</attribute>
<attribute key="attribute3">value3</attribute>
</person>
有任何想法吗?
解决方案
尝试:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//section[identifier/@code='S001']">
<person>
<xsl:for-each select="//td[1]">
<attribute key="{.}"><xsl:value-of select="../td[2]"/></attribute>
</xsl:for-each>
</person>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
备注:这是已编辑帖子的解决方案。
推荐阅读
- bash - 如何在bash中查看文件退出
- python - 如何在 Python 中列出每隔十个数字的列表
- json - JSON jolt null 值在转换后添加到嵌套数组
- android - 如何打开文件管理器并获取任何文件的绝对路径?
- makefile - GNU make 路径替换(目录展平)
- reactjs - react-beautiful-dnd 嵌套列表拖到父级
- java - 修复模块 {a} 不会“打开 {package}”到 Eclipse 中的模块 {B}
- ruby-on-rails - Rails 6 / Kubernetes:Readiness HTTP 探测失败,状态码:403
- google-cloud-platform - 从 GKE 集群访问 kubeflow 管道
- c++ - 形状内部、外部和形状上的点