获取冒号后的值 - 键 [英] Get the value after the colon - keys
问题描述
我对此几乎有相同的问题:在每个最后一个冒号后获取值.就我而言,我需要对 :A: 和 :B: 的每次出现进行迭代,:B: 是 :A: 的子项.在我的代码中,我使用了一个我不太熟悉的调用模板.但是,我需要探索 xslt 中的其他功能/元素.
I have almost the same issue for this one: Get value after each last colon. In my case, I need to iterate for every occurrence of :A: and :B:, :B: is a child of :A:. In my code, I am using a call-template which is I'm not very familiar with. But, I need to explore the other functionalities/elements in xslt.
这是我的示例测试文件:
Here is my sample test file:
<Record>
:A:This is sample only 1
:B:This is sample only 2
:B:This is sample only 3
:A:This is sample only 4
:B:This is sample only 5
</Record>
预期输出:
<Record>
<Detail>
<FieldA>This is sample only 1</FieldA>
<Trans>
<Group>
<FieldB>This is sample only 2</FieldB>
</Group>
<Group>
<FieldB>This is sample only 3</FieldB>
</Group>
</Trans>
</Detail>
<Detail>
<FieldA>This is sample only 4</FieldA>
<Trans>
<Group>
<FieldB>This is sample only 5</FieldB>
</Group>
</Trans>
</Detail>
<Record>
对于每次出现:A:,我需要创建一个
记录,然后对于每个:B: 将创建一个
记录.这是我的 XSLT 代码,
For every occurrence of :A:, I need to create a <Detail>
record, then for each :B: will create a <Group>
record. And here is my XSLT code,
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<xsl:call-template name="FormatXML">
<xsl:with-param name="Input">
<Record>
<xsl:for-each select="tokenize(.,':A:')">
<xsl:analyze-string select="." regex=":([0-9A-Za-z]+):(.*)\n">
<xsl:matching-substring>
<xsl:variable name="FieldA">
<xsl:if test="regex-group(1) = 'A'">
<FieldB>
<xsl:value-of select="regex-group(2)"/>
</FieldB>
</xsl:if>
</xsl:variable>
<xsl:for-each select="tokenize(.,':B:')">
<xsl:variable name="FieldB">
<xsl:if test="regex-group(1) = 'B'">
<FieldB>
<xsl:value-of select="regex-group(2)"/>
</FieldB>
</xsl:if>
</xsl:variable>
<Group>
<FieldB>
<xsl:value-of select="$FieldB"/>
</FieldB>
</Group>
</xsl:for-each>
<Detail>
<FieldA>
<xsl:value-of select="$FieldA"/>
</FieldA>
</Detail>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:for-each>
</Record>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="FormatXML">
<xsl:param name="Input"/>
<xsl:apply-templates select="$Input"/>
</xsl:template>
<xsl:template match="/Record">
<xsl:copy>
<xsl:apply-templates select="Detail"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Detail">
<xsl:copy>
<xsl:copy-of select="FieldA"/>
<Trans>
<xsl:apply-templates select="Group"/>
</Trans>
</xsl:copy>
</xsl:template>
<xsl:template match="Group">
<xsl:copy>
<xsl:apply-templates select="FieldB"/>
</xsl:copy>
</xsl:template>
我的 xslt 代码不起作用.如果我在代码中遗漏了什么,任何人都可以帮助我.非常感谢您的反馈.
My xslt code is not working. Can anyone help me if what I missed in my code. Your feedback is highly appreciated.
谢谢!
推荐答案
我建议采用完全不同的方法:首先将字符串转换为单个元素,这样您就可以:
I would suggest a radically different approach: first convert the string to individual elements, so that you have:
<FieldA>This is sample only 1</FieldA>
<FieldB>This is sample only 2</FieldB>
<FieldB>This is sample only 3</FieldB>
<FieldA>This is sample only 4</FieldA>
<FieldB>This is sample only 5</FieldB>
在一个变量中.
然后将变量中的元素分组并按照您想要的方式输出:
Then group the elements in the variable and output them the way you want them:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<!-- convert strings to elements -->
<xsl:variable name="temp" as="element()*">
<xsl:analyze-string select="." regex="^:([AB]):(.*)$" flags="m">
<xsl:matching-substring>
<xsl:element name="Field{regex-group(1)}">
<xsl:value-of select="regex-group(2)" />
</xsl:element>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<!-- output -->
<xsl:copy>
<!-- group elements -->
<xsl:for-each-group select="$temp" group-starting-with="FieldA">
<Detail>
<xsl:copy-of select="current-group()[1]" />
<Trans>
<xsl:for-each select="current-group()[position() ge 2]">
<Group>
<xsl:copy-of select="." />
</Group>
</xsl:for-each>
</Trans>
</Detail>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<小时>
演示:http://xsltransform.net/gVhD8Ro/1
这篇关于获取冒号后的值 - 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!