使用XSLT将零填充到字符串

时间:2017-01-11 16:51:17

标签: php xml xslt

我有一个XSLT(版本1.0)来挑选一个包含数字的字符串,而不是所有字符串都有前导零。问题是这个字符串现在用于按字母顺序对元素进行排序。

理想情况下,运行XSLT(以及其他更改)应该更改:

<atom name="EADUnitID" type="text" size="short">BODA.4.3.60</atom>
<atom name="EADUnitID" type="text" size="short">BODA.4.3.61</atom>

要:

<field name="heirarchy_sequence">BODA.04.03.60</field>
<field name="heirarchy_sequence">BODA.04.03.60</field>

如何重写我们的XSLT,以便在必要时用零填充此字符串中的数字? 另一个小打嗝是字符串中的文本和数字之间的分隔符并不总是'。'有时它可能是' - '。

当我宣布PHP命名空间时,我想我可能会使用PHP函数来分解字符串和sprinntf以格式化数字部分,但我觉得这不起作用...

我的XSLT看起来像这样:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xlink="http://www.w3.org/2001/XMLSchema-instance">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="add">
    <add>
        <xsl:apply-templates />
    </add>
</xsl:template>
<xsl:template match="doc">
    <xsl:copy>
        ...     
            <field name="heirarchy_sequence">
                 <xsl:value-of select="atom[@name='EADUnitID'][normalize-space()]"/>
            </field>
        ...
        </xsl:copy>
</xsl:template>

它正在改变的XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xml_split:root xmlns:xml_split="http://xmltwig.com/xml_split">
<doc name="record">
    <atom name="irn" type="text" size="short">6135</atom>
    <atom name="ObjectType" type="text" size="short">Archives</atom>
    <atom name="EADLevelAttribute" type="text" size="short">Item</atom>
    <atom name="EADUnitID" type="text" size="short">BODA.4.3.60</atom>
  </doc>
  <doc name="record">
    <atom name="irn" type="text" size="short">6136</atom>
    <atom name="ObjectType" type="text" size="short">Archives</atom>
    <atom name="EADLevelAttribute" type="text" size="short">Item</atom>
    <atom name="EADUnitID" type="text" size="short">BODA.4.3.61</atom>
  </doc>
  </xml_split:root>

修改1

从xml示例中删除了不必要的</table>

编辑2 - 变体示例

<atom name="EADUnitID" type="text" size="short">gls-1-1-1</atom>
<atom name="EADUnitID" type="text" size="short">gls-1-1-2</atom>

1 个答案:

答案 0 :(得分:3)

假设您正在使用name := "myproject" version := "1.0" scalaVersion := "2.10.4" val sparkVersion = "1.6.2" mainClass in assembly := Some("xxx.spark.yyy.Main") resolvers += Resolver.sojava.lang.NoClassDefFoundErrornatypeRepo("releases") resolvers += "Spark Packages Repo" at "https://dl.bintray.com/spark-packages/maven" resolvers += "Akka Snapshot Repository" at "http://repo.akka.io/snapshots/" resolvers += "Artifactory" at "http://artifacts.kameleoon.net:8081/artifactory/sbt/" resolvers += "Sbt plugins" at "https://dl.bintray.com/sbt/sbt-plugin-releases" resolvers += "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/" resolvers += Resolver.url("artifactory", url("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns) libraryDependencies ++= Seq( "org.apache.spark" %% "spark-core" % sparkVersion % "provided", "org.apache.spark" %% "spark-sql" % sparkVersion % "provided", "com.datastax.spark" %% "spark-cassandra-connector" % "1.6.0", "org.apache.spark" %% "spark-mllib" % sparkVersion % "provided", "org.hibernate" % "hibernate-core" % "4.3.11.Final", "org.hibernate" % "hibernate-entitymanager" % "4.3.11.Final", compilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full), "org.json" % "json" % "20160810", "org.joda" % "joda-convert" % "1.2", "jfree" % "jfreechart" % "1.0.13", "commons-io" % "commons-io" % "2.4", "com.google.guava" % "guava" % "20.0", "jfree" % "jfreechart" % "1.0.13", "org.bytedeco" % "javacv" % "1.2", "org.datavec" % "datavec-data-codec" % "0.7.2", "org.datavec" % "datavec-spark_2.10" % "0.7.2", "org.datavec" % "datavec-api" % "0.7.2", "org.deeplearning4j" % "deeplearning4j-core" % "0.7.2", "org.deeplearning4j" % "deeplearning4j-nn" % "0.7.2", "org.deeplearning4j" % "dl4j-spark_2.10" % "0.7.2", "org.jblas" % "jblas" % "1.2.4" ) assemblyMergeStrategy in assembly := { case PathList("org", "joda", "time", "base", "BaseDateTime.class") => MergeStrategy.first case PathList("com", "esotericsoftware", "minlog", "Log.class") => MergeStrategy.first case PathList("org", "apache", xs @ _*) => MergeStrategy.last case PathList("com", "google", xs @ _*) => MergeStrategy.last case PathList("META-INF", xs @ _*) => MergeStrategy.rename case "about.html" => MergeStrategy.rename case x => val oldStrategy = (assemblyMergeStrategy in assembly).value oldStrategy(x) } 处理器,您可以执行以下操作:

libxslt

转换:

<xsl:template match="atom[@name='EADUnitID']">
    <field name="heirarchy_sequence">
        <xsl:variable name="tokens" select="str:tokenize(., '.-')" />
            <xsl:value-of select="$tokens[1]" />
        <xsl:for-each select="$tokens[position() > 1]">
            <xsl:text>.</xsl:text>
            <xsl:value-of select="str:align(., '0000', 'right')"/>
        </xsl:for-each>
    </field>
</xsl:template>

成:

<atom name="EADUnitID" type="text" size="short">BODA.4.3.60</atom>
<atom name="EADUnitID" type="text" size="short">BODA.4.3.61</atom>
<atom name="EADUnitID" type="text" size="short">gls-1-1-1</atom>
<atom name="EADUnitID" type="text" size="short">gls-1-1-2</atom>

请参阅:http://exslt.org/str/index.html