简体   繁体   English

使用xslt 2.0将字符串模式转换为XML

[英]converting string pattern to XML using xslt 2.0

MSH.1:MSH.2:MSH.3:PID.1:PID.2:ORC.1:ORC.3

above string pattern I would like to transform into below XML format 上面的字符串模式,我想转换成下面的XML格式

<filters>
<element group="MSH">
  <location path="MSH.1"/>
  <location path="MSH.2"/>
  <location path="MSH.3"/>
</element>
<element group="PID">
  <location path="PID.1"/>
  <location path="PID.2"/>
</element>
<element group="ORC">
  <location path="ORC.1"/>
  <location path-"ORC.3"/>
</elment>
</filters>

Can I have sample XSLT to achieve above scenario 我可以使用示例XSLT来实现上述方案吗

Note : I am making use of XSLT 2.0 ans saxon transformer 注意:我正在使用XSLT 2.0 ans撒克逊变压器

Assuming you start the transformation in Saxon with parameters 假设您使用参数在Saxon中开始转换

-it:main in=MSH.1:MSH.2:MSH.3:PID.1:PID.2:ORC.1:ORC.3

try 尝试

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="in"/>

<xsl:template name="main">
<filters>
  <xsl:variable name="tokens" select="tokenize($in, ':')"/>
  <xsl:for-each-group select="$tokens" group-by="substring-before(., '.')"/>
   <element group="{current-grouping-key()}">
     <xsl:for-each select="current-group()">
       <location path="{.}"/>
     </xsl:for-each>
   </element>
  </xsl:for-each-group>
</filters>
<xsl:template>

</xsl:stylesheet>

You can use the unparsed-text() function in XSLT 2.0 to load a document as a string variable. 您可以在XSLT 2.0中使用unparsed-text()函数将文档作为字符串变量加载。

You would then probably use the xsl:analyze-string instruction to split the string, first on the ":" character, then possibly on the period character. 然后,您可能会使用xsl:analyze-string指令来拆分字符串,首先在":"字符上,然后在句点字符上。

XSLT is meant to manipulate XML (into something else). XSLT旨在操纵XML(转换成其他东西)。 So you will have to XMLize your string first, at minimum 因此,您至少必须首先将字符串XML化

<xmlize>MSH.1:MSH.2:MSH.3:PID.1:PID.2:ORC.1:ORC.3</xmlize>

You can then have an XSLT matching on the root element and you can use `tokenize()' to process your string. 然后,您可以在根元素上具有XSLT匹配项,并且可以使用“ tokenize()”来处理您的字符串。 See this discussion 查看此讨论

Doing this with XSLT is a bit weird; 使用XSLT这样做有点奇怪; can't you use another script/language that has String manipulation functions with regular expressions splitting ? 您不能使用具有正则表达式拆分功能的String操纵功能的其他脚本/语言吗?

nb: when you "xmlize", do not forget to escape '<' '>' and '&' if appropriate nb:在“ xmlize”时,请不要忘记转义“ <”,“>”和“&”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM