测试数据:
<?xml version="1.0" encoding="UTF-8"?>
<Declaration>
<AcceptanceDateTime>2011-05-01</AcceptanceDateTime>
<ID>CA 0011112347</ID>
<GovernmentAgencyGoodsItems>
<GovernmentAgencyGoodsItem>
<SequenceNumeric>1</SequenceNumeric>
<Description>PHENOLIC RESIN COATING (THIXON IS BRAND) THIXON P-14 5 GAL/PAIL</Description>
</GovernmentAgencyGoodsItem>
<GovernmentAgencyGoodsItem>
<SequenceNumeric>2</SequenceNumeric>
<Description>THIXON 520</Description>
</GovernmentAgencyGoodsItem>
</GovernmentAgencyGoodsItems>
</Declaration>
我希望转换后的结果如下:
<?xml version="1.0" encoding="UTF-8"?>
<Declaration>
<DECL_DATE>2011-05-01</DECL_DATE>
<DECL_ID>CA 0011112347</DECL_ID>
<GoodsItems>
<GoodsItem>
<SEQ>1</SEQ>
<Descp1>PHENOLIC RESIN COATING (THIXON IS </Descp1>
<Descp2>BRAND) THIXON P-14 5 GAL/PAIL</Descp2>
<Descp3></Descp3>
</GoodsItem>
<GoodsItem>
<SEQ>2</SEQ>
<Descp1>THIXON 520</Descp1>
<Descp2></Descp2>
<Descp3></Descp3>
</GoodsItem>
</GoodsItems>
</Declaration>
是否可以使用“xsl:analyze-string”转换XML以将每35个字符拆分成组,并将元素的值从DESCP1设置为DESCP3。
<xsl:analyze-string select="Description" regex="\b[\s\S]{0,35}\b">
<xsl:matching-substring>
<Descp1>
<xsl:value-of select="regex-group(1)" />
</Descp1>
<Descp2>
<xsl:value-of select="regex-group(2)" />
</Descp2>
<Descp3>
<xsl:value-of select="regex-group(3)" />
</Descp3>
</xsl:matching-substring>
</xsl:analyze-string>
请帮帮我。
答案 0 :(得分:0)
我认为这会给你大致正确的效果:
<xsl:variable name="pat">.{1,35}(\s|$)</xsl:variable>
<xsl:analyze-string select="$in" regex="{$pat}">
<xsl:matching-substring>
<xsl:element name="desc{position()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:matching-substring>
</xsl:analyze-string>
您可能希望对其进行微调,例如控制哪些字符被视为空格,保留/丢弃空白分隔符,或处理生成三行以上的情况。
您的代码不会起作用(至少)2个原因: